[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/a-pavlov/libhttpserver/master/src/http_utils.cpp [Back]  [Original]

/*
     This file is part of libhttpserver
     Copyright (C) 2011, 2012, 2013, 2014 Sebastiano Merlino

     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later version.

     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.

     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
     USA
*/

#include 
#include 
#include 
#if defined(__MINGW32__) || defined(__CYGWIN32__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include 
#include 
#else
#include 
#endif
#include 
#include 
#include 
#include 
#include "string_utilities.hpp"
#include "http_utils.hpp"

#pragma GCC diagnostic ignored "-Warray-bounds"
#define CHECK_BIT(var,pos) ((var) & (1sin_port;
            case AF_INET6:
                return ((struct sockaddr_in *)sa)->sin_port;
            default:
                return 0;
        }
    }
    return 0;
}

size_t http_unescape (char *val)
{
    char *rpos = val;
    char *wpos = val;
    unsigned int num;

    while ('\0' != *rpos)
    {
        switch (*rpos)
        {
            case '+':
                *wpos = ' ';
                wpos++;
                rpos++;
                break;
            case '%':
                if ( (1 == sscanf (&rpos[1],
                    "%2x", &num)) ||
                    (1 == sscanf (&rpos[1],
                    "%2X", &num))
                )
                {
                    *wpos = (unsigned char) num;
                    wpos++;
                    rpos += 3;
                    break;
                }
            /* intentional fall through! */
            default:
                *wpos = *rpos;
                wpos++;
                rpos++;
        }
    }
    *wpos = '\0'; /* add 0-terminator */
    return wpos - val; /* = strlen(val) */
}

ip_representation::ip_representation(const struct sockaddr* ip)
{
    std::fill(pieces, pieces + 16, 0);
    if(ip->sa_family == AF_INET)
    {
        ip_version = http_utils::IPV4;
        for(int i=0;isin_addr))[i];
        }
    }
    else
    {
        ip_version = http_utils::IPV6;
        for(int i=0;isin6_addr))[i] +
                16 * ((u_char*)&(((struct sockaddr_in6 *)ip)->sin6_addr))[i+1];
        }
    }
    mask = DEFAULT_MASK_VALUE;
}

ip_representation::ip_representation(const std::string& ip)
{
    std::vector parts;
    mask = DEFAULT_MASK_VALUE;
    std::fill(pieces, pieces + 16, 0);
    if(ip.find(':') != std::string::npos) //IPV6
    {
        ip_version = http_utils::IPV6;
        string_utilities::string_split(ip, parts, ':', false);
        int y = 0;
        for(unsigned int i = 0; i < parts.size(); i++)
        {
            if(parts[i] != "*" && parts[i] != "")
            {
                if(parts[i].size() < 4)
                {
                    stringstream ss;
                    ss pieces[i] < b.pieces[i]
        )
            return true;
    }
    return false;
}

size_t load_file (const char* filename, char** content)
{
    ifstream fp(filename, ios::in | ios::binary | ios::ate);
    if(fp.is_open())
    {
        int size = fp.tellg();
        *content = (char*) malloc(size * sizeof(char));
        fp.seekg(0, ios::beg);
        fp.read(*content, size);
        fp.close();
        return size;
    }
    else
        throw file_access_exception();
}

char* load_file (const char *filename)
{
    char* content = NULL;
    load_file(filename, &content);
    return content;
}

void dump_header_map(std::ostream &os, const std::string &prefix,
                     const std::map &map)
{
    std::map::const_iterator it = map.begin(); 
    std::map::const_iterator end = map.end();

    if (map.size()) {
        os 

Web Proxy Viewer  |  New URL  |  Original Page