File: ap_http_client.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (296 lines) | stat: -rw-r--r-- 8,056 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************************
*                         Goggles Audio Player Library                         *
********************************************************************************
*           Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved      *
*                               ---                                            *
* This program is free software: you can redistribute it and/or modify         *
* it under the terms of the GNU General Public License as published by         *
* the Free Software Foundation, either version 3 of the License, or            *
* (at your option) any later version.                                          *
*                                                                              *
* This program 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 General Public License for more details.                                 *
*                                                                              *
* You should have received a copy of the GNU General Public License            *
* along with this program.  If not, see http://www.gnu.org/licenses.           *
********************************************************************************/
#include "ap_defs.h"
#include "ap_common.h"
#include "ap_socket.h"
#include "ap_connect.h"
#include "ap_buffer_base.h"
#include "ap_buffer_io.h"
#include "ap_http_response.h"
#include "ap_http_client.h"

using namespace ap;

namespace ap {


HttpHost::HttpHost(const FXString & url) {
  set(url);
  }

void HttpHost::clear() {
  name.clear();
  port=0;
  ssl=false;
  }

FXbool HttpHost::set(const FXString & url) {
  FXbool   ns = FXString::comparecase(FXURL::scheme(url), "https") == 0;
  FXString nn = FXURL::host(url);
  FXint    np = FXURL::port(url, ns ? 443 : 80);
  if (name!=nn || port!=np || ssl!=ns) {
    name.adopt(nn);
    port=np;
    ssl=ns;
    return true;
    }
  return false;
  }




HttpClient::HttpClient(ConnectionFactory * c) :  connection(c), options(0) {
  }

void HttpClient::setConnectionFactory(ConnectionFactory * c) {
  if (connection) {
      delete connection;
      connection = nullptr;
      }
  connection=c;
  }

HttpClient::~HttpClient() {
  close();
  delete connection;
  }

#ifdef HAVE_ZLIB
void HttpClient::setAcceptEncoding(const FXuchar opts) {
  options = (options&~AcceptEncodingGZip) | (opts&AcceptEncodingGZip);
  }
#else
void HttpClient::setAcceptEncoding(const FXuchar) {
  options = (options&~AcceptEncodingGZip); // always turn off
  }
#endif


void HttpClient::close() {
  GM_DEBUG_PRINT("[http] close()\n");

  // Shutdown communication
  ap::Socket * s = dynamic_cast<ap::Socket*>(io.attached());
  if (s) s->shutdown();

  io.close();
  }

void HttpClient::discard() {
  GM_DEBUG_PRINT("[http] discard()\n");
  if (flags&ConnectionClose) {
    close();
    }
  else if (io.isOpen()) {
    HttpResponse::discard();
    }
  }

FXbool HttpClient::open_connection() {
  GM_DEBUG_PRINT("[http] open connection\n");
  FXIO * stream = nullptr;

  if (connection==nullptr)
    connection = new ConnectionFactory();

  if (options&UseProxy)
    stream = connection->open(proxy.name.text(),proxy.port, proxy.ssl);
  else
    stream = connection->open(server.name.text(),server.port, server.ssl);

  if (stream) {
    GM_DEBUG_PRINT("[http] connected\n");
    io.attach(stream);
    return true;
    }
  GM_DEBUG_PRINT("[http] connection failure\n");
  return false;
  }

void HttpClient::reset(FXbool forceclose){
  if (forceclose)
    close();
  else
    discard();
  clear();
  }


FXbool HttpClient::request(const FXchar * method,const FXString & url,const FXString & header,const FXString & message) {
  GM_DEBUG_PRINT("[http] request(\"%s\",\"%s\")\n",method,url.text());
  FXString command,path,query;

  // Set Server Host
  FXbool host_changed = server.set(url);
  if (options&UseProxy)
    host_changed = false;

  // Reset Client
  reset(host_changed);

  if (FXString::comparecase(method,"HEAD")==0) {
    flags|=HeadRequest;
    }

  // Open connection if necessary
  if (io.isOpen()==false && !open_connection()){
    server.clear();
    return false;
    }

  // Extract path and query
  path  = FXURL::path(url);
  if (path.empty())
    path = "/";

  query = FXURL::query(url);
  if (!query.empty())
    path += "?" + query;


//  fxmessage("path: %s\n",path.text());

  // Method + Path
//  if (flags&HttpProxy)
//    command = method + url  + "\r\n"
  //else
  command = method;
  command += " " + path + " HTTP/1.1\r\n";

  // Add Host
  command += "Host: " + server.name + "\r\n";

  // Add Content Length
  if (message.length())
    command += "Content-Length: " + FXString::value(message.length()) + "\r\n";

  // Add Accept Encoding
  if (options&AcceptEncodingGZip)
    command += "Accept-Encoding: gzip\r\n";

  // Additional headers
  command+=header;

  // End of headers
  command += "\r\n";

  // Add body
  if (message.length())
    command += message;

  // Send Command
  return io.write(command);
  }


FXbool HttpClient::basic(const FXchar*    method,
                         FXString         url,
                         const FXString & header,
                         const FXString & content,
                         FXString*        moved/*=nullptr*/) {

  int redirect = 0;

  if (request(method,url,header,content)) {
    do {
      switch(parse()) {
        case HTTP_RESPONSE_INFORMATIONAL:
          {
            if (status.code==HTTP_CONTINUE) {
              continue;
              }
            break;
          }
        case HTTP_RESPONSE_REDIRECT     :
          {
            // 304 - Document not changed. We're done
            // 305 - Need to use a proxy. Currently not handled
            // 306 - Unused
            if (status.code==HTTP_NOT_MODIFIED || status.code==HTTP_USE_PROXY || status.code==HTTP_306)
              return true;

            url = getHeader("location");

            // No url given, done here
            if (url.empty())
              return false;

            // Prevent infinite redirects
            if (redirect>10)
              return false;

            // Save moved url (only on first permanent redirect)
            if (status.code==HTTP_MOVED_PERMANENTLY && redirect==0 && moved) {
              *moved=url;
              }
            // Don't do automatic redirections for non GET/HEAD requests
            if (FXString::comparecase(method,"GET") && FXString::comparecase(method,"HEAD"))
              return true;

            if (!request(method,url,header,content)) {
              return false;
              }
            redirect++;
            continue;
            break;
          }
        case HTTP_RESPONSE_CLIENT_ERROR  :
          {
            if (status.code==HTTP_UNAUTHORIZED) {

              FXString user     = FXURL::username(url);
              FXString password = FXURL::password(url);

              if (user.empty() || password.empty())
                return true;

              FXString challenge = getHeader("www-authenticate");

              if (FXString::comparecase(challenge,"basic",5)==0) {
                FXString auth = "Authorization: Basic " + Base64Encoder::encodeString(user+":"+password) + "\r\n";

                if (!request(method,url,header+auth,content)) {
                  return false;
                  }
                continue;
                }
//              else if (comparecase(challenge,"digest",6)==0){
//                FXASSERT(0);
//                }
              }
          } break;

        case HTTP_RESPONSE_FAILED:  /* something went wrong */
          {
            GM_DEBUG_PRINT("[http] response failed\n");
            return false;
            break;
          }
        default: break;
        }
      return true;
      }
    while(1);
    }
  return false;
  }


}