| 1 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | Name |
| 4 | osnetcli.cpp - Windows implemention of HTTP client requests |
| 5 | Function |
| 6 | |
| 7 | Notes |
| 8 | |
| 9 | Modified |
| 10 | 04/10/10 MJRoberts - Creation |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include "t3std.h" |
| 15 | #include "os.h" |
| 16 | #include "osifcnet.h" |
| 17 | #include "vmnet.h" |
| 18 | #include "vmisaac.h" |
| 19 | #include "vmfile.h" |
| 20 | |
| 21 | |
| 22 | /* ------------------------------------------------------------------------ */ |
| 23 | /* |
| 24 | * HTTP client |
| 25 | */ |
| 26 | |
| 27 | /* ISAAC context for generating random multipart boundary strings */ |
| 28 | static isaacctx isaac; |
| 29 | static int isaac_inited = FALSE; |
| 30 | |
| 31 | /* |
| 32 | * Send an HTTP request |
| 33 | */ |
| 34 | int OS_HttpClient::request(int opts, |
| 35 | const char *host, unsigned short portno, |
| 36 | const char *verb, const char *resource, |
| 37 | const char *send_headers, size_t send_headers_len, |
| 38 | OS_HttpPayload *payload, CVmStream *reply, |
| 39 | char **reply_headers, char **location, |
| 40 | const char *ua) |
| 41 | { |
| 42 | HINTERNET hconn = 0; |
| 43 | HINTERNET hsession = 0; |
| 44 | HINTERNET hreq = 0; |
| 45 | int ok = FALSE; |
| 46 | static const char *accept_types[] = { |
| 47 | "text/*", |
| 48 | "image/*", |
| 49 | "application/octet-stream", |
| 50 | 0 |
| 51 | }; |
| 52 | char *gen_headers = 0; |
| 53 | size_t gen_hdrlen = 0; |
| 54 | DWORD flags; |
| 55 | char *putdata = 0; |
| 56 | size_t putlen = 0; |
| 57 | int part_cnt = 0; |
| 58 | char **part_hdrs = 0; |
| 59 | int status = ErrOther; |
| 60 | |
| 61 | /* check for custom headers from the caller */ |
| 62 | if (send_headers != 0) |
| 63 | { |
| 64 | /* remove any trailing \r\n from the caller's headers */ |
| 65 | while (send_headers_len >= 2 |
| 66 | && send_headers[send_headers_len - 2] == '\r' |
| 67 | && send_headers[send_headers_len - 1] == '\n') |
| 68 | send_headers_len -= 2; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | /* no custom headers from the caller - use an empty string */ |
| 73 | send_headers = ""; |
| 74 | send_headers_len = 0; |
| 75 | } |
| 76 | |
| 77 | /* apply our default UA string if we don't have one */ |
| 78 | if (ua == 0 || ua[0] == '\0') |
| 79 | ua = "TadsWinHttpClient/001"; |
| 80 | |
| 81 | /* presume we won't return any headers */ |
| 82 | if (reply_headers != 0) |
| 83 | *reply_headers = 0; |
| 84 | |
| 85 | /* open an internet connection */ |
| 86 | hconn = InternetOpen(ua, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); |
| 87 | if (hconn == 0) |
| 88 | { |
| 89 | status = ErrNet; |
| 90 | goto done; |
| 91 | } |
| 92 | |
| 93 | /* connect to the server */ |
| 94 | hsession = InternetConnect( |
| 95 | hconn, host, portno, "", "", INTERNET_SERVICE_HTTP, 0, 0); |
| 96 | if (hsession == 0) |
| 97 | { |
| 98 | status = ErrNoConn; |
| 99 | goto done; |
| 100 | } |
| 101 | |
| 102 | /* set up our base flags */ |
| 103 | flags = INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD |
| 104 | | INTERNET_FLAG_NO_CACHE_WRITE; |
| 105 | |
| 106 | /* add the "no auto redirect" flag if desired */ |
| 107 | if (location != 0) |
| 108 | { |
| 109 | flags |= INTERNET_FLAG_NO_AUTO_REDIRECT; |
| 110 | *location = 0; |
| 111 | } |
| 112 | |
| 113 | /* add the "secure sockets" flag if we're using https */ |
| 114 | if ((opts & OptHTTPS) != 0) |
| 115 | flags |= INTERNET_FLAG_SECURE; |
| 116 | |
| 117 | /* if there's a payload, build the send buffer */ |
| 118 | if (payload != 0 && payload->count_items() > 0) |
| 119 | { |
| 120 | /* get the nubmer of payload items */ |
| 121 | part_cnt = payload->count_items(); |
| 122 | |
| 123 | /* presume we're not going to do special POST encoding */ |
| 124 | int post_encoding = FALSE; |
| 125 | |
| 126 | /* check the verb */ |
| 127 | if (stricmp(verb, "POST") == 0) |
| 128 | { |
| 129 | /* |
| 130 | * For POST, we're sending form data, but there are three |
| 131 | * different formats for form data: |
| 132 | * |
| 133 | * - If the payload consists of a single file item with an |
| 134 | * empty string as the name, it means that the caller has |
| 135 | * provided a pre-encoded content body that we should send |
| 136 | * exactly as given, with no further format encoding. |
| 137 | * |
| 138 | * - If the payload consists entirely of simple name/value |
| 139 | * fields, send in the application/x-www-url-encoded format. |
| 140 | * |
| 141 | * - If we have any file uploads among the form fields, send as |
| 142 | * multipart/form-data. |
| 143 | */ |
| 144 | if (payload->count_items() == 1 |
| 145 | && payload->get(0)->name[0] == '\0') |
| 146 | { |
| 147 | /* |
| 148 | * The caller has provided a pre-encoded content body. |
| 149 | * Send it as given - in other words, use the PUT handling. |
| 150 | */ |
| 151 | } |
| 152 | else if (payload->is_multipart()) |
| 153 | { |
| 154 | /* |
| 155 | * We have files, so use the multipart/form-data format. |
| 156 | * Note that we're doing special POST encoding. |
| 157 | */ |
| 158 | post_encoding = TRUE; |
| 159 | |
| 160 | /* |
| 161 | * First, generate a boundary string. It should be |
| 162 | * sufficient to generate a long random string - the |
| 163 | * chances of a long enough random string appearing within |
| 164 | * the data should be negligible. Initialize our RNG if we |
| 165 | * haven't already done so. |
| 166 | */ |
| 167 | if (!isaac_inited) |
| 168 | { |
| 169 | os_gen_rand_bytes((uchar *)isaac.rsl, sizeof(isaac.rsl)); |
| 170 | isaac_init(&isaac, TRUE); |
| 171 | isaac_inited = TRUE; |
| 172 | } |
| 173 | |
| 174 | /* generate the boundary string */ |
| 175 | char boundary[128]; |
| 176 | sprintf(boundary, "--%08lx-%08lx-%08lx-%08lx-%08lx-%08lx", |
| 177 | isaac_rand(&isaac), isaac_rand(&isaac), |
| 178 | isaac_rand(&isaac), isaac_rand(&isaac), |
| 179 | isaac_rand(&isaac), GetTickCount()); |
| 180 | size_t boundlen = strlen(boundary); |
| 181 | |
| 182 | /* figure the length: start with the opening boundary */ |
| 183 | putlen = boundlen; |
| 184 | |
| 185 | /* |
| 186 | * run through the items: for each item, build its item |
| 187 | * header, and figure its contribution to the data length |
| 188 | */ |
| 189 | part_hdrs = new char*[part_cnt]; |
| 190 | int i; |
| 191 | for (i = 0 ; i < part_cnt ; ++i) |
| 192 | { |
| 193 | /* get this item */ |
| 194 | OS_HttpPayloadItem *item = payload->get(i); |
| 195 | |
| 196 | /* check to see which kind of item we have */ |
| 197 | if (item->stream != 0) |
| 198 | { |
| 199 | /* file item */ |
| 200 | part_hdrs[i] = t3sprintf_alloc( |
| 201 | "\r\n" |
| 202 | "Content-disposition: form-data; " |
| 203 | "name=\"%s\"; filename=\"%s\"\r\n" |
| 204 | "Content-type: %s\r\n" |
| 205 | "Content-transfer-encoding: %s\r\n" |
| 206 | "\r\n", |
| 207 | item->name, |
| 208 | item->val, |
| 209 | item->mime_type, |
| 210 | memicmp(item->mime_type, "text/", 5) == 0 |
| 211 | ? "8bit" : "binary"); |
| 212 | |
| 213 | /* add the stream length */ |
| 214 | putlen += item->stream->get_len(); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | /* ordinary field item */ |
| 219 | part_hdrs[i] = t3sprintf_alloc( |
| 220 | "\r\n" |
| 221 | "Content-disposition: form-data; name=\"%s\"\r\n" |
| 222 | "Content-type: text/plain; encoding=utf-8\r\n" |
| 223 | "\r\n", |
| 224 | item->name); |
| 225 | |
| 226 | /* add the length of the value */ |
| 227 | putlen += strlen(item->val); |
| 228 | } |
| 229 | |
| 230 | /* if we couldn't allocate a part header, give up */ |
| 231 | if (part_hdrs[i] == 0) |
| 232 | { |
| 233 | status = ErrNoMem; |
| 234 | goto done; |
| 235 | } |
| 236 | |
| 237 | /* add this item's header length */ |
| 238 | putlen += strlen(part_hdrs[i]); |
| 239 | |
| 240 | /* add the CR-LF and boundary at the end of the item */ |
| 241 | putlen += 2 + boundlen; |
| 242 | } |
| 243 | |
| 244 | /* add the final "--\r\n" boundary ending */ |
| 245 | putlen += 4; |
| 246 | |
| 247 | /* allocate the buffer */ |
| 248 | putdata = (char *)t3malloc(putlen); |
| 249 | if (putdata == 0) |
| 250 | { |
| 251 | status = ErrNoMem; |
| 252 | goto done; |
| 253 | } |
| 254 | |
| 255 | /* build the buffer - start with the opening boundary */ |
| 256 | char *p = putdata; |
| 257 | memcpy(p, boundary, boundlen); |
| 258 | p += boundlen; |
| 259 | |
| 260 | /* add each item */ |
| 261 | for (i = 0 ; i < part_cnt ; ++i) |
| 262 | { |
| 263 | /* get this item */ |
| 264 | OS_HttpPayloadItem *item = payload->get(i); |
| 265 | |
| 266 | /* add the header */ |
| 267 | strcpy(p, part_hdrs[i]); |
| 268 | p += strlen(p); |
| 269 | |
| 270 | /* add the content */ |
| 271 | if (item->stream != 0) |
| 272 | { |
| 273 | err_try |
| 274 | { |
| 275 | /* copy the entire stream into the buffer */ |
| 276 | size_t l = item->stream->get_len(); |
| 277 | item->stream->read_bytes(p, l); |
| 278 | |
| 279 | /* skip past it */ |
| 280 | p += l; |
| 281 | } |
| 282 | err_catch(exc) |
| 283 | { |
| 284 | status = ErrReadFile; |
| 285 | goto done; |
| 286 | } |
| 287 | err_end; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | /* copy the value into the buffer */ |
| 292 | strcpy(p, item->val); |
| 293 | p += strlen(p); |
| 294 | } |
| 295 | |
| 296 | /* add the trailing CR-LF and boundary */ |
| 297 | memcpy(p, "\r\n", 2); |
| 298 | memcpy(p + 2, boundary, boundlen); |
| 299 | p += 2 + boundlen; |
| 300 | } |
| 301 | |
| 302 | /* add the "--\r\n" at the end of the last boundary */ |
| 303 | memcpy(p, "--\r\n", 4); |
| 304 | p += 4; |
| 305 | |
| 306 | /* prepare the content type header */ |
| 307 | gen_headers = t3sprintf_alloc( |
| 308 | "Content-type: multipart/form-data; boundary=%s\r\n" |
| 309 | "Content-length: %d\r\n", |
| 310 | boundary + 2, putlen); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | /* |
| 315 | * No files are involved, so send in x-www-url-encoded |
| 316 | * format. Note that we're doing special POST encoding. |
| 317 | */ |
| 318 | post_encoding = TRUE; |
| 319 | |
| 320 | /* URL-encode the data */ |
| 321 | putdata = payload->urlencode(putlen); |
| 322 | |
| 323 | /* prepare the content type header */ |
| 324 | gen_headers = t3sprintf_alloc( |
| 325 | "Content-type: application/x-www-form-urlencoded\r\n" |
| 326 | "Content-length: %d\r\n", |
| 327 | putlen); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* if we didn't do special POST encoding, prepare the content body */ |
| 332 | if (!post_encoding) |
| 333 | { |
| 334 | /* |
| 335 | * for any verb other than POST, we can only send a single file |
| 336 | * item - verify that that's what we have |
| 337 | */ |
| 338 | OS_HttpPayloadItem *f; |
| 339 | if (part_cnt > 1 || (f = payload->get(0))->stream == 0) |
| 340 | { |
| 341 | status = ErrParams; |
| 342 | goto done; |
| 343 | } |
| 344 | |
| 345 | /* build the headers for the request to send the file item */ |
| 346 | gen_headers = t3sprintf_alloc( |
| 347 | "Content-type: %s\r\n" |
| 348 | "Content-length: %lu\r\n", |
| 349 | f->mime_type, f->stream->get_len()); |
| 350 | |
| 351 | /* allocate the put buffer */ |
| 352 | putlen = f->stream->get_len(); |
| 353 | putdata = (char *)t3malloc(putlen); |
| 354 | if (putdata == 0) |
| 355 | { |
| 356 | status = ErrNoMem; |
| 357 | goto done; |
| 358 | } |
| 359 | |
| 360 | /* read the stream into the buffer */ |
| 361 | err_try |
| 362 | { |
| 363 | f->stream->read_bytes(putdata, putlen); |
| 364 | } |
| 365 | err_catch(exc) |
| 366 | { |
| 367 | status = ErrReadFile; |
| 368 | goto done; |
| 369 | } |
| 370 | err_end; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* add the custom headers, if provided */ |
| 375 | if (send_headers_len != 0) |
| 376 | { |
| 377 | /* add the custom headers to our generated headers, if any */ |
| 378 | char *new_headers = t3sprintf_alloc( |
| 379 | "%s%.*s\r\n", |
| 380 | gen_headers != 0 ? gen_headers : "", |
| 381 | (int)send_headers_len, send_headers); |
| 382 | |
| 383 | /* |
| 384 | * if we generated our own headers, we're done with those now, as |
| 385 | * we've just copied those into the full header string |
| 386 | */ |
| 387 | if (gen_headers != 0) |
| 388 | t3free(gen_headers); |
| 389 | |
| 390 | /* use the full combined generated + custom header string */ |
| 391 | gen_headers = new_headers; |
| 392 | } |
| 393 | |
| 394 | /* if we have headers, calculate the length */ |
| 395 | if (gen_headers != 0) |
| 396 | gen_hdrlen = strlen(gen_headers); |
| 397 | |
| 398 | /* open the request */ |
| 399 | hreq = HttpOpenRequest( |
| 400 | hsession, verb, resource, 0, 0, accept_types, flags, 0); |
| 401 | if (hreq == 0) |
| 402 | { |
| 403 | status = ErrNoConn; |
| 404 | goto done; |
| 405 | } |
| 406 | |
| 407 | /* send the request */ |
| 408 | if (!HttpSendRequest(hreq, gen_headers, gen_hdrlen, putdata, putlen)) |
| 409 | { |
| 410 | status = ErrNoConn; |
| 411 | goto done; |
| 412 | } |
| 413 | |
| 414 | /* get the status code */ |
| 415 | DWORD http_status, clen, siz = sizeof(http_status); |
| 416 | if (!HttpQueryInfo(hreq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, |
| 417 | &http_status, &siz, 0)) |
| 418 | { |
| 419 | status = ErrNet; |
| 420 | goto done; |
| 421 | } |
| 422 | |
| 423 | /* use the HTTP status as the return status */ |
| 424 | status = (int)http_status; |
| 425 | |
| 426 | /* if the caller wants the headers, return them */ |
| 427 | if (reply_headers != 0) |
| 428 | { |
| 429 | /* |
| 430 | * first, do the query without a buffer, to see how much space we |
| 431 | * need to allocate |
| 432 | */ |
| 433 | DWORD sz = 0; |
| 434 | if (!HttpQueryInfo(hreq, HTTP_QUERY_RAW_HEADERS_CRLF, 0, &sz, 0) |
| 435 | && GetLastError() == ERROR_INSUFFICIENT_BUFFER) |
| 436 | { |
| 437 | /* allocate the buffer */ |
| 438 | *reply_headers = new char[sz + 1]; |
| 439 | |
| 440 | /* retrieve the headers */ |
| 441 | if (HttpQueryInfo(hreq, HTTP_QUERY_RAW_HEADERS_CRLF, |
| 442 | *reply_headers, &sz, 0)) |
| 443 | { |
| 444 | /* success - null-terminate the buffer */ |
| 445 | (*reply_headers)[sz] = '\0'; |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | /* failed - discard the headers */ |
| 450 | delete [] *reply_headers; |
| 451 | status = ErrGetHdrs; |
| 452 | goto done; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* make sure we got a success code or (if allowed) a redirect */ |
| 458 | if (!(status == 200 |
| 459 | || (status == 301 && location != 0))) |
| 460 | goto done; |
| 461 | |
| 462 | /* if it's a redirect, pass back the location */ |
| 463 | if (status == 301) |
| 464 | { |
| 465 | /* query the Location header from the response */ |
| 466 | char lbuf[4096]; |
| 467 | siz = sizeof(lbuf); |
| 468 | if (!HttpQueryInfo(hreq, HTTP_QUERY_LOCATION, lbuf, &siz, 0)) |
| 469 | { |
| 470 | status = ErrNet; |
| 471 | goto done; |
| 472 | } |
| 473 | |
| 474 | /* pass it back to our caller as an allocated string */ |
| 475 | *location = lib_copy_str(lbuf, (size_t)siz); |
| 476 | } |
| 477 | |
| 478 | /* get the content length */ |
| 479 | int varclen = FALSE; |
| 480 | siz = sizeof(clen); |
| 481 | if (!HttpQueryInfo(hreq, |
| 482 | HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, |
| 483 | &clen, &siz, 0) || clen == 0) |
| 484 | { |
| 485 | /* no explicit content length */ |
| 486 | clen = 0; |
| 487 | varclen = TRUE; |
| 488 | } |
| 489 | |
| 490 | /* read the results */ |
| 491 | err_try |
| 492 | { |
| 493 | for (;;) |
| 494 | { |
| 495 | DWORD bytes_read; |
| 496 | char buf[1024]; |
| 497 | |
| 498 | /* read the next chunk - abort on error */ |
| 499 | DWORD cur = (clen > sizeof(buf) || varclen ? sizeof(buf) : clen); |
| 500 | if (!InternetReadFile(hreq, buf, cur, &bytes_read)) |
| 501 | goto done; |
| 502 | |
| 503 | /* if that's EOF, we're done */ |
| 504 | if (bytes_read == 0) |
| 505 | break; |
| 506 | |
| 507 | /* store the current chunk in the output stream, if we have one */ |
| 508 | if (reply != 0) |
| 509 | reply->write_bytes(buf, (size_t)bytes_read); |
| 510 | |
| 511 | /* deduct the amount read from the remaining amount, if known */ |
| 512 | if (!varclen) |
| 513 | clen -= bytes_read; |
| 514 | } |
| 515 | } |
| 516 | err_catch (exc) |
| 517 | { |
| 518 | /* failed to write to the reply object */ |
| 519 | status = ErrWriteFile; |
| 520 | goto done; |
| 521 | } |
| 522 | err_end; |
| 523 | |
| 524 | /* success */ |
| 525 | ok = TRUE; |
| 526 | |
| 527 | done: |
| 528 | /* clean up data buffers for the sent data */ |
| 529 | if (putdata != 0) |
| 530 | t3free(putdata); |
| 531 | if (gen_headers != 0) |
| 532 | t3free(gen_headers); |
| 533 | if (part_hdrs != 0) |
| 534 | { |
| 535 | for (int i = 0 ; i < part_cnt ; ++i) |
| 536 | { |
| 537 | if (part_hdrs[0] != 0) |
| 538 | t3free(part_hdrs[i]); |
| 539 | } |
| 540 | delete [] part_hdrs; |
| 541 | } |
| 542 | |
| 543 | /* clean up the connection */ |
| 544 | if (hreq != 0) |
| 545 | InternetCloseHandle(hreq); |
| 546 | if (hsession != 0) |
| 547 | InternetCloseHandle(hsession); |
| 548 | if (hconn != 0) |
| 549 | InternetCloseHandle(hconn); |
| 550 | |
| 551 | /* return the status code */ |
| 552 | return status; |
| 553 | } |
| 554 | |
| 555 |