Public Member Functions | Public Attributes | Private Attributes

HTTPConnection Struct Reference

#include <httpclnt.h>

Collaboration diagram for HTTPConnection:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 HTTPConnection (const char *agent, const URL &url)
Socketget_socket (const AddressList &addr)
void close ()
string send_request (const URL &url, const AddressList &addr, const char *verb, const char *data, size_t len, bool close_connection)

Public Attributes

const char * _agent
URL _url

Private Attributes

INConnect_pConnect

Detailed Description

Definition at line 170 of file httpclnt.h.


Constructor & Destructor Documentation

HTTPConnection::HTTPConnection ( const char *  agent,
const URL url 
)

Definition at line 116 of file httpclnt.cpp.

 :  _agent(agent),
    _url(url),
    _pConnect(NULL)
{
}

Member Function Documentation

void HTTPConnection::close (  )

Definition at line 131 of file httpclnt.cpp.

References _pConnect.

Referenced by send_request().

{
    if (_pConnect) {
        delete _pConnect;
        _pConnect = NULL;
    }
}

Here is the caller graph for this function:

Socket & HTTPConnection::get_socket ( const AddressList addr )

Definition at line 123 of file httpclnt.cpp.

References _pConnect.

Referenced by send_request().

{
    if (!_pConnect)
        _pConnect = new INConnect(addr);

    return *_pConnect;
}

Here is the caller graph for this function:

string HTTPConnection::send_request ( const URL url,
const AddressList addr,
const char *  verb,
const char *  data,
size_t  len,
bool  close_connection 
)

Definition at line 140 of file httpclnt.cpp.

References _agent, URL::_hostname, URL::_path, URL::_port, _stricmp, _strnicmp, BUFFER_LEN, close(), get_socket(), SocketReader::read(), SocketReader::read_line(), Socket::send(), and sscanf_s.

Referenced by XMLRPCClient::request_post().

{
    Socket& socket = get_socket(addr);

     // send the HTML request header
    ostringstream buffer;

    buffer << verb << " " << url._path << " HTTP/1.1\r\n";
    buffer << "User-Agent: " << _agent << "\r\n";
    buffer << "Host: " << url._hostname << ":" << url._port << "\r\n";
    buffer << "Content-length: " << len << "\r\n";
    buffer << "Content-type: text/xml\r\n";
//  buffer << "Cache-Control: no-cache\r\n";
    buffer << "\r\n";

    const string& header = buffer.str();
    socket.send(header.c_str(), header.length());

     // send the request body
    socket.send(data, len);


     // read response line
    SocketReader reader(socket);

    char line[BUFFER_LEN];

    if (reader.read_line(line, BUFFER_LEN) <= 0)
        throw Exception("missing HTML server response");

    char version[12];
    int code;
    char msg[BUFFER_LEN];

#ifdef __STDC_WANT_SECURE_LIB__
    if (sscanf_s(line, "%s %d %s", version, _countof(version), &code, msg, _countof(msg)) != 3)
#else
    if (sscanf(line, "%s %d %s", version, &code, msg) != 3)
#endif
        throw Exception("invalid HTML server response");

    if (code != 200)
        throw Exception(line);


     // read the response header
    int content_length = 0;
    string content_type;

    for(;;) {
        if (reader.read_line(line, BUFFER_LEN) <= 0)
            break;

        if (!_strnicmp(line, "Content-length:", 15))
            content_length = atoi(line+15);
        else if (!_strnicmp(line, "Content-type:", 13))
            content_type = line+14;
        else if (!_stricmp(line, "Connection: close"))
            close_connection = true;
    }

    if (!content_type.empty() && _strnicmp(content_type.c_str(),"text/xml",8)) {
        char b[BUFFER_LEN];
        sprintf(b, "illegal content type: %s", content_type.c_str());
        throw Exception(b);
    }


     // read the response body
    string ret;

    if (content_length) {
        ret.resize(content_length);
        reader.read(&ret.at(0), content_length);
    }

    if (close_connection)
        close();

    return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 177 of file httpclnt.h.

Referenced by send_request().

Definition at line 183 of file httpclnt.h.

Referenced by close(), and get_socket().

Definition at line 178 of file httpclnt.h.


The documentation for this struct was generated from the following files: