pad.mattdiesel.co.uk

Snippet - Http Request

Http Request (C++)


Created 2015-02-18 10:42:55.271790 by Matt.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Wininet.h>

#include <string>

namespace http
{


std::string get(std::string host, int port, std::string page)
{
    HINTERNET hInternet = ::InternetOpenA("md639 MarsLander", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (!hInternet)
        throw std::runtime_error("Couldn't open the internet.");

    HINTERNET hConnect = ::InternetConnectA(hInternet, host.c_str(), (INTERNET_PORT)port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
    if (!hConnect) {
        ::InternetCloseHandle(hInternet);
        throw std::runtime_error("Unable to connect to the server.");
    }

    // Should be application/json, but accept text as a backup
    LPCSTR aszAccept[] = {
        "application/json",
        "text/*",
        NULL
    };

    HINTERNET hRequest = ::HttpOpenRequestA(hConnect, NULL, page.c_str(), NULL, NULL, aszAccept, INTERNET_FLAG_NO_COOKIES, NULL);
    if (!hRequest) {
        ::InternetCloseHandle(hConnect);
        ::InternetCloseHandle(hInternet);
        throw std::runtime_error("Error opening request.");
    }

    BOOL r = ::HttpSendRequestA(hRequest, NULL, 0, NULL, 0);
    if (!r) {
        ::InternetCloseHandle(hRequest);
        ::InternetCloseHandle(hConnect);
        ::InternetCloseHandle(hInternet);

        throw std::runtime_error("Error sending request.");
    }

    DWORD cbContent;
    DWORD cbcbContent = sizeof(cbContent);
    r = HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, &cbContent, &cbcbContent, NULL);
    if (!r) {
        int n = GetLastError();

        if (n == ERROR_HTTP_HEADER_NOT_FOUND) {
            cbContent = 1024;
        }
        else {
            ::InternetCloseHandle(hRequest);
            ::InternetCloseHandle(hConnect);
            ::InternetCloseHandle(hInternet);

            throw std::runtime_error("Couldn't query content length.");
        }
    }

    std::string ret = "";
    char* lpBuffer = new char[cbContent+1];

    DWORD cbRead = 0;
    do {
        ::InternetReadFile(hRequest, lpBuffer, cbContent, &cbRead);

        ret.append(lpBuffer, cbRead);
    } while (cbRead != 0);

    delete[] lpBuffer;

    ::InternetCloseHandle(hRequest);
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);

    return ret;
}


}


#else

#include <string>
#include <stdexcept>
#include <sstream>

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

namespace http
{


int create_tcp_socket();
std::string get_ip( std::string host );
std::string build_get_query( std::string host, std::string page );


std::string get( std::string host, int port, std::string page )
{
    sockaddr_in* remote;
    int sock;
    int tmpres;
    std::string ip;
    std::string get;
    std::string buf;
    buf.resize( BUFSIZ + 1 );

    sock = create_tcp_socket();
    ip = get_ip( host );

    remote = new sockaddr_in;
    remote->sin_family = AF_INET;

    tmpres = inet_pton( AF_INET, ip.c_str(), &( remote->sin_addr.s_addr ) );
    if ( tmpres < 0 ) {
        throw std::runtime_error( "Can't set remote->sin_addr.s_addr" );
    }
    else if ( tmpres == 0 ) {
        throw std::invalid_argument( "IP Address not valid." );
    }
    remote->sin_port = htons( port );

    if ( connect( sock, ( sockaddr* )remote, sizeof( sockaddr ) ) < 0 ) {
        throw std::runtime_error( "Could not connect to socket." );
    }
    get = build_get_query( host, page );

    //Send the query to the server
    unsigned int sent = 0;
    while ( sent < get.length() ) {
        tmpres = send( sock, get.c_str() + sent, get.length() - sent, 0 );
        if ( tmpres == -1 ) {
            throw std::runtime_error( "Could not send request." );
        }

        sent += tmpres;
    }

    //now it is time to receive the page
    unsigned int htmlstart = 0;
    bool seen = false;
    std::ostringstream oss;

    while ( ( tmpres = recv( sock, &buf[0], BUFSIZ, 0 ) ) > 0 ) {
        if ( !seen ) {
            htmlstart = buf.find( "\r\n\r\n" );

            if ( htmlstart != std::string::npos ) {
                seen = true;

                oss << &buf.c_str()[htmlstart + 4];
            }
        }
        else {
            oss << buf;
        }

        buf.clear();
    }

    delete remote;
    close( sock );

    return oss.str();
}


int create_tcp_socket()
{
    int sock;

    sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if ( sock < 0 ) {
        throw std::runtime_error( "Can't create TCP socket" );
    }

    return sock;
}


std::string get_ip( std::string host )
{
    hostent* hent;
    const int iplen = 15;
    std::string ip;
    ip.resize( iplen + 1 );

    hent = gethostbyname( host.c_str() );
    if ( !hent ) {
        throw std::runtime_error( "Unable to get IP." );
    }

    if ( !inet_ntop( AF_INET, hent->h_addr_list[0], &ip[0], iplen ) ) {
        throw std::runtime_error( "Unable to resolve host." );
    }

    return ip;
}

std::string build_get_query( std::string host, std::string page )
{
    std::ostringstream oss;

    oss << "GET " << ( ( page[0] == '/' ) ? "" : "/" ) << page << " HTTP/1.0\r\n"
        << "Host: " << host << "\r\n"
        << "User-Agent: md639 MarsLander\r\n\r\n";

    return oss.str();
}


}

#endif