| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/inetfile/Chttpget.cpp $ |
| 21 | * $Revision: 1.4 $ |
| 22 | * $Date: 2001/01/13 21:48:46 $ |
| 23 | * $Author: icculus $ |
| 24 | * |
| 25 | * HTTP Client class (get only) |
| 26 | * |
| 27 | * $Log: Chttpget.cpp,v $ |
| 28 | * Revision 1.4 2001/01/13 21:48:46 icculus |
| 29 | * patched to (re)compile on win32. |
| 30 | * |
| 31 | * Revision 1.3 2000/06/29 06:41:23 icculus |
| 32 | * mad commits. |
| 33 | * |
| 34 | * Revision 1.2 2000/06/03 14:30:21 icculus |
| 35 | * 1.4 code merge and pthread->SDL thread conversion. |
| 36 | * |
| 37 | * Revision 1.1.1.1 2000/04/18 00:00:38 icculus |
| 38 | * initial checkin |
| 39 | * |
| 40 | * |
| 41 | * 26 10/22/99 3:40p Kevin |
| 42 | * mac merge fixes |
| 43 | * |
| 44 | * 25 10/21/99 9:28p Jeff |
| 45 | * B.A. Macintosh code merge |
| 46 | * |
| 47 | * 24 9/08/99 6:37p Jeff |
| 48 | * fixed http/ftp downloading for Linux, should all work fine now. |
| 49 | * |
| 50 | * 23 8/22/99 12:32a Jeff |
| 51 | * fixed select calls for Linux. Ported Kevin's new http stuff to Linux |
| 52 | * |
| 53 | * 22 8/21/99 9:14p Kevin |
| 54 | * Added support for redirection |
| 55 | * |
| 56 | * 21 8/21/99 6:33p Kevin |
| 57 | * Fixed Proxy Stuff |
| 58 | * |
| 59 | * 20 8/21/99 6:48a Jeff |
| 60 | * Linux port |
| 61 | * |
| 62 | * 19 8/20/99 3:01p Kevin |
| 63 | * Added support for Proxies (I hope!) |
| 64 | * |
| 65 | * 18 8/15/99 6:38p Jeff |
| 66 | * fixed compile error |
| 67 | * |
| 68 | * 17 8/15/99 6:26p Kevin |
| 69 | * |
| 70 | * 16 4/14/99 1:20a Jeff |
| 71 | * fixed case mismatched #includes |
| 72 | * |
| 73 | * 15 3/03/99 12:28a Nate |
| 74 | * sped up something or other when the connection is done |
| 75 | * |
| 76 | * 14 2/03/99 4:20p Kevin |
| 77 | * Got multiplayer working with .mn3 files, and setup autodownloading |
| 78 | * |
| 79 | * 13 1/27/99 5:49p Kevin |
| 80 | * |
| 81 | * 12 1/27/99 5:38p Kevin |
| 82 | * |
| 83 | * 11 12/30/98 12:15p Kevin |
| 84 | * Auto Mission Download system |
| 85 | * |
| 86 | * 10 10/12/98 4:59p Kevin |
| 87 | * Added delay to thread when cancelled... |
| 88 | * |
| 89 | * 9 10/12/98 4:49p Nate |
| 90 | * More fixes |
| 91 | * |
| 92 | * 8 10/12/98 1:54p Nate |
| 93 | * Fixed bug |
| 94 | * |
| 95 | * 7 10/12/98 11:30a Kevin |
| 96 | * More memory stuff |
| 97 | * |
| 98 | * 6 10/08/98 12:59p Nate |
| 99 | * fixed cancel |
| 100 | * |
| 101 | * 5 10/08/98 9:57a Kevin |
| 102 | * made transfer cancellable |
| 103 | * |
| 104 | * 4 7/31/98 12:19p Nate |
| 105 | * Fixed http abort problem. |
| 106 | * |
| 107 | * 3 7/31/98 11:57a Kevin |
| 108 | * Added new functions for getting state |
| 109 | * |
| 110 | * 2 6/01/98 10:10a Kevin |
| 111 | * Added DLL connection interface and auto update DLL |
| 112 | * |
| 113 | * 1 5/27/98 9:52a Kevin |
| 114 | * |
| 115 | * 1 5/25/98 5:31p Kevin |
| 116 | * Initial version |
| 117 | * |
| 118 | * $NoKeywords: $ |
| 119 | */ |
| 120 | |
| 121 | #ifdef WIN32 |
| 122 | #include <windows.h> |
| 123 | #include <winsock.h> |
| 124 | #include <process.h> |
| 125 | #endif |
| 126 | |
| 127 | #include <string.h> |
| 128 | #include <stdio.h> |
| 129 | #include <stdlib.h> |
| 130 | |
| 131 | #include "inetgetfile.h" |
| 132 | #include "Chttpget.h" |
| 133 | |
| 134 | #ifndef WIN32 |
| 135 | #include "mem.h" |
| 136 | #else |
| 137 | #define mem_malloc(a) malloc(a) |
| 138 | #define mem_free(a) free(a) |
| 139 | #endif |
| 140 | |
| 141 | #if defined(POSIX) |
| 142 | |
| 143 | #include "SDL_thread.h" |
| 144 | |
| 145 | inline void Sleep(int millis) { |
| 146 | struct timeval tv; |
| 147 | tv.tv_sec = 0; |
| 148 | tv.tv_usec = millis * 1000; |
| 149 | select(0, NULL, NULL, NULL, &tv); |
| 150 | } |
| 151 | #endif |
| 152 | |
| 153 | #define NW_AGHBN_CANCEL 1 |
| 154 | #define NW_AGHBN_LOOKUP 2 |
| 155 | #define NW_AGHBN_READ 3 |
| 156 | |
| 157 | #if !defined(POSIX) |
| 158 | static void HTTPObjThread(void *obj); |
| 159 | static void __cdecl http_gethostbynameworker(void *parm); |
| 160 | #else |
| 161 | static int HTTPObjThread(void *obj); |
| 162 | static int http_gethostbynameworker(void *parm); |
| 163 | #endif |
| 164 | |
| 165 | static int http_Asyncgethostbyname(uint32_t *ip, int command, char *hostname); |
| 166 | |
| 167 | #if !defined(POSIX) |
| 168 | void HTTPObjThread(void *obj) |
| 169 | #else |
| 170 | int HTTPObjThread(void *obj) |
| 171 | #endif |
| 172 | { |
| 173 | ((ChttpGet *)obj)->WorkerThread(); |
| 174 | ((ChttpGet *)obj)->m_Aborted = true; |
| 175 | // OutputDebugString("http transfer exiting....\n"); |
| 176 | |
| 177 | #if defined(POSIX) |
| 178 | return 0; |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | void ChttpGet::AbortGet() { |
| 183 | #ifdef WIN32 |
| 184 | OutputDebugString("Aborting....\n"); |
| 185 | #endif |
| 186 | m_Aborting = true; |
| 187 | while (!m_Aborted) |
| 188 | Sleep(50); // Wait for the thread to end |
| 189 | #ifdef WIN32 |
| 190 | OutputDebugString("Aborted....\n"); |
| 191 | #endif |
| 192 | } |
| 193 | |
| 194 | ChttpGet::ChttpGet(char *URL, char *localfile, char *proxyip, uint16_t proxyport) { |
| 195 | m_ProxyEnabled = true; |
| 196 | m_ProxyIP = proxyip; |
| 197 | m_ProxyPort = proxyport; |
| 198 | GetFile(URL, localfile); |
| 199 | } |
| 200 | |
| 201 | ChttpGet::ChttpGet(char *URL, char *localfile) { |
| 202 | m_ProxyEnabled = false; |
| 203 | GetFile(URL, localfile); |
| 204 | } |
| 205 | |
| 206 | void ChttpGet::PrepSocket(char *URL) { |
| 207 | |
| 208 | m_DataSock = socket(AF_INET, SOCK_STREAM, 0); |
| 209 | if (INVALID_SOCKET == m_DataSock) { |
| 210 | m_State = HTTP_STATE_SOCKET_ERROR; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | #ifdef WIN32 |
| 215 | unsigned long arg = 1; |
| 216 | ioctlsocket(m_DataSock, FIONBIO, &arg); |
| 217 | #else // WIN32 |
| 218 | fcntl(m_DataSock, F_SETFL, fcntl(m_DataSock, F_GETFL, 0) | O_NONBLOCK); |
| 219 | #endif |
| 220 | |
| 221 | char *pURL = URL; |
| 222 | if (strnicmp(URL, "http:", 5) == 0) { |
| 223 | pURL += 5; |
| 224 | while (*pURL == '/') { |
| 225 | pURL++; |
| 226 | } |
| 227 | } |
| 228 | // There shouldn't be any : in this string |
| 229 | if (strchr(pURL, ':')) { |
| 230 | m_State = HTTP_STATE_URL_PARSING_ERROR; |
| 231 | return; |
| 232 | } |
| 233 | // read the filename by searching backwards for a / |
| 234 | // then keep reading until you find the first / |
| 235 | // when you found it, you have the host and dir |
| 236 | char *filestart = NULL; |
| 237 | char *dirstart = NULL; |
| 238 | for (int i = strlen(pURL); i >= 0; i--) { |
| 239 | if (pURL[i] == '/') { |
| 240 | if (!filestart) { |
| 241 | filestart = pURL + i + 1; |
| 242 | dirstart = pURL + i + 1; |
| 243 | strcpy(m_szFilename, filestart); |
| 244 | } else { |
| 245 | dirstart = pURL + i + 1; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | if ((dirstart == NULL) || (filestart == NULL)) { |
| 250 | m_State = HTTP_STATE_URL_PARSING_ERROR; |
| 251 | return; |
| 252 | } else { |
| 253 | strcpy(m_szDir, dirstart); //,(filestart-dirstart)); |
| 254 | // m_szDir[(filestart-dirstart)] = NULL; |
| 255 | strncpy(m_szHost, pURL, (dirstart - pURL)); |
| 256 | m_szHost[(dirstart - pURL) - 1] = '\0'; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void ChttpGet::GetFile(char *URL, char *localfile) { |
| 261 | m_DataSock = INVALID_SOCKET; |
| 262 | m_iBytesIn = 0; |
| 263 | m_iBytesTotal = 0; |
| 264 | m_State = HTTP_STATE_STARTUP; |
| 265 | ; |
| 266 | m_Aborting = false; |
| 267 | m_Aborted = false; |
| 268 | |
| 269 | strncpy(m_URL, URL, MAX_URL_LEN - 1); |
| 270 | m_URL[MAX_URL_LEN - 1] = 0; |
| 271 | |
| 272 | LOCALFILE = fopen(localfile, "wb"); |
| 273 | if (NULL == LOCALFILE) { |
| 274 | m_State = HTTP_STATE_CANT_WRITE_FILE; |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | PrepSocket(URL); |
| 279 | |
| 280 | #ifdef WIN32 |
| 281 | if (NULL == _beginthread(HTTPObjThread, 0, this)) { |
| 282 | m_State = HTTP_STATE_INTERNAL_ERROR; |
| 283 | return; |
| 284 | } |
| 285 | #elif defined(POSIX) |
| 286 | // pthread_t thread; |
| 287 | SDL_Thread *thread; |
| 288 | |
| 289 | if (!inet_LoadThreadLib()) { |
| 290 | m_State = HTTP_STATE_INTERNAL_ERROR; |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // if(df_pthread_create(&thread,NULL,HTTPObjThread,this)!=0) |
| 295 | thread = SDL_CreateThread(HTTPObjThread, "httpget", this); |
| 296 | if (thread == NULL) { |
| 297 | m_State = HTTP_STATE_INTERNAL_ERROR; |
| 298 | return; |
| 299 | } |
| 300 | #endif |
| 301 | } |
| 302 | |
| 303 | ChttpGet::~ChttpGet() { |
| 304 | if (m_DataSock != INVALID_SOCKET) { |
| 305 | shutdown(m_DataSock, 2); |
| 306 | #if !defined(POSIX) |
| 307 | closesocket(m_DataSock); |
| 308 | #else |
| 309 | close(m_DataSock); |
| 310 | #endif |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | int ChttpGet::GetStatus() { return m_State; } |
| 315 | |
| 316 | uint32_t ChttpGet::GetBytesIn() { return m_iBytesIn; } |
| 317 | |
| 318 | uint32_t ChttpGet::GetTotalBytes() { return m_iBytesTotal; } |
| 319 | |
| 320 | void ChttpGet::WorkerThread() { |
| 321 | char szCommand[1000]; |
| 322 | char *p; |
| 323 | int irsp = 0; |
| 324 | ConnectSocket(); |
| 325 | if (m_Aborting) { |
| 326 | fclose(LOCALFILE); |
| 327 | return; |
| 328 | } |
| 329 | if (m_State != HTTP_STATE_CONNECTED) { |
| 330 | fclose(LOCALFILE); |
| 331 | return; |
| 332 | } |
| 333 | snprintf(szCommand, sizeof(szCommand), "GET %s%s HTTP/1.1\nAccept: */*\nAccept-Encoding: deflate\nHost: %s\n\n\n", |
| 334 | m_ProxyEnabled ? "" : "/", m_ProxyEnabled ? m_URL : m_szDir, m_szHost); |
| 335 | send(m_DataSock, szCommand, strlen(szCommand), 0); |
| 336 | p = GetHTTPLine(); |
| 337 | if (p && strnicmp("HTTP/", p, 5) == 0) { |
| 338 | char *pcode; |
| 339 | pcode = strchr(p, ' ') + 1; |
| 340 | if (!pcode) { |
| 341 | m_State = HTTP_STATE_UNKNOWN_ERROR; |
| 342 | fclose(LOCALFILE); |
| 343 | return; |
| 344 | } |
| 345 | pcode[3] = '\0'; |
| 346 | irsp = atoi(pcode); |
| 347 | |
| 348 | if (irsp == 0) { |
| 349 | m_State = HTTP_STATE_UNKNOWN_ERROR; |
| 350 | fclose(LOCALFILE); |
| 351 | return; |
| 352 | } |
| 353 | if (irsp == 301) { |
| 354 | |
| 355 | // This is a redirection! woohoo, what fun we are going to have. |
| 356 | // Next thing we need to do is find where it's redirected to. |
| 357 | // We do that by looking for a "Location: xxxx" line. |
| 358 | |
| 359 | int idataready = 0; |
| 360 | do { |
| 361 | p = GetHTTPLine(); |
| 362 | if (p == NULL) { |
| 363 | m_State = HTTP_STATE_UNKNOWN_ERROR; |
| 364 | fclose(LOCALFILE); |
| 365 | return; |
| 366 | } |
| 367 | if (*p == '\0') { |
| 368 | idataready = 1; |
| 369 | break; |
| 370 | } |
| 371 | if (strnicmp(p, "Location:", strlen("Location:")) == 0) { |
| 372 | char *s = strchr(p, ' ') + 1; |
| 373 | |
| 374 | // Then, once we've found that, we close the sockets & reissue the whole freakin request. |
| 375 | shutdown(m_DataSock, 2); |
| 376 | |
| 377 | #ifdef WIN32 |
| 378 | closesocket(m_DataSock); |
| 379 | #else |
| 380 | close(m_DataSock); |
| 381 | #endif |
| 382 | |
| 383 | m_DataSock = INVALID_SOCKET; |
| 384 | |
| 385 | // New location to look at is in 's' |
| 386 | PrepSocket(s); |
| 387 | WorkerThread(); |
| 388 | return; |
| 389 | } |
| 390 | } while (!idataready); |
| 391 | } |
| 392 | if (irsp == 200) { |
| 393 | int idataready = 0; |
| 394 | do { |
| 395 | p = GetHTTPLine(); |
| 396 | if (p == NULL) { |
| 397 | m_State = HTTP_STATE_UNKNOWN_ERROR; |
| 398 | fclose(LOCALFILE); |
| 399 | return; |
| 400 | } |
| 401 | if (*p == '\0') { |
| 402 | idataready = 1; |
| 403 | break; |
| 404 | } |
| 405 | if (strnicmp(p, "Content-Length:", strlen("Content-Length:")) == 0) { |
| 406 | char *s = strchr(p, ' ') + 1; |
| 407 | p = s; |
| 408 | if (s) { |
| 409 | while (*s) { |
| 410 | if (!isdigit(*s)) { |
| 411 | *s = '\0'; |
| 412 | } |
| 413 | s++; |
| 414 | }; |
| 415 | m_iBytesTotal = atoi(p); |
| 416 | } |
| 417 | } |
| 418 | } while (!idataready); |
| 419 | ReadDataChannel(); |
| 420 | return; |
| 421 | } |
| 422 | m_State = HTTP_STATE_FILE_NOT_FOUND; |
| 423 | fclose(LOCALFILE); |
| 424 | return; |
| 425 | } else { |
| 426 | m_State = HTTP_STATE_UNKNOWN_ERROR; |
| 427 | fclose(LOCALFILE); |
| 428 | return; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | int ChttpGet::ConnectSocket() { |
| 433 | // HOSTENT *he; |
| 434 | uint32_t ip; |
| 435 | SERVENT *se; |
| 436 | SOCKADDR_IN hostaddr; |
| 437 | |
| 438 | int rcode = 0; |
| 439 | |
| 440 | if (m_Aborting) |
| 441 | return 0; |
| 442 | |
| 443 | ip = inet_addr((const char *)m_szHost); |
| 444 | |
| 445 | if (ip == INADDR_NONE) { |
| 446 | http_Asyncgethostbyname(&ip, NW_AGHBN_LOOKUP, m_szHost); |
| 447 | rcode = 0; |
| 448 | do { |
| 449 | if (m_Aborting) { |
| 450 | http_Asyncgethostbyname(&ip, NW_AGHBN_CANCEL, m_szHost); |
| 451 | return 0; |
| 452 | } |
| 453 | rcode = http_Asyncgethostbyname(&ip, NW_AGHBN_READ, m_szHost); |
| 454 | } while (rcode == 0); |
| 455 | } |
| 456 | |
| 457 | if (rcode == -1) { |
| 458 | m_State = HTTP_STATE_HOST_NOT_FOUND; |
| 459 | return 0; |
| 460 | } |
| 461 | // m_ControlSock |
| 462 | if (m_Aborting) |
| 463 | return 0; |
| 464 | se = getservbyname("http", NULL); |
| 465 | if (m_Aborting) |
| 466 | return 0; |
| 467 | if (se == NULL) { |
| 468 | hostaddr.sin_port = htons(80); |
| 469 | } else { |
| 470 | hostaddr.sin_port = se->s_port; |
| 471 | } |
| 472 | hostaddr.sin_family = AF_INET; |
| 473 | // ip = htonl(ip); |
| 474 | memcpy(&hostaddr.sin_addr, &ip, 4); |
| 475 | |
| 476 | if (m_ProxyEnabled) { |
| 477 | // This is on a proxy, so we need to make sure to connect to the proxy machine |
| 478 | ip = inet_addr((const char *)m_ProxyIP); |
| 479 | |
| 480 | if (ip == INADDR_NONE) { |
| 481 | http_Asyncgethostbyname(&ip, NW_AGHBN_LOOKUP, m_ProxyIP); |
| 482 | rcode = 0; |
| 483 | do { |
| 484 | if (m_Aborting) { |
| 485 | http_Asyncgethostbyname(&ip, NW_AGHBN_CANCEL, m_ProxyIP); |
| 486 | return 0; |
| 487 | } |
| 488 | rcode = http_Asyncgethostbyname(&ip, NW_AGHBN_READ, m_ProxyIP); |
| 489 | } while (rcode == 0); |
| 490 | |
| 491 | if (rcode == -1) { |
| 492 | m_State = HTTP_STATE_HOST_NOT_FOUND; |
| 493 | return 0; |
| 494 | } |
| 495 | } |
| 496 | // Use either the proxy port or 80 if none specified |
| 497 | hostaddr.sin_port = htons(m_ProxyPort ? m_ProxyPort : 80); |
| 498 | // Copy the proxy address... |
| 499 | memcpy(&hostaddr.sin_addr, &ip, 4); |
| 500 | } |
| 501 | // Now we will connect to the host |
| 502 | fd_set wfds; |
| 503 | |
| 504 | timeval timeout; |
| 505 | timeout.tv_sec = 0; |
| 506 | timeout.tv_usec = 0; |
| 507 | int serr = connect(m_DataSock, (SOCKADDR *)&hostaddr, sizeof(SOCKADDR)); |
| 508 | int cerr = WSAGetLastError(); |
| 509 | if (serr) { |
| 510 | #if defined(POSIX) |
| 511 | while ((cerr == WSAEALREADY) || (cerr == WSAEINVAL) || (cerr == WSAEWOULDBLOCK) || (cerr == EINPROGRESS)) |
| 512 | #else |
| 513 | while ((cerr == WSAEALREADY) || (cerr == WSAEINVAL) || (cerr == WSAEWOULDBLOCK)) |
| 514 | #endif |
| 515 | { |
| 516 | FD_ZERO(&wfds); |
| 517 | FD_SET(m_DataSock, &wfds); |
| 518 | if (select(m_DataSock + 1, NULL, &wfds, NULL, &timeout)) { |
| 519 | serr = 0; |
| 520 | break; |
| 521 | } |
| 522 | if (m_Aborting) |
| 523 | return 0; |
| 524 | serr = connect(m_DataSock, (SOCKADDR *)&hostaddr, sizeof(SOCKADDR)); |
| 525 | if (serr == 0) |
| 526 | break; |
| 527 | cerr = WSAGetLastError(); |
| 528 | if (cerr == WSAEISCONN) { |
| 529 | serr = 0; |
| 530 | break; |
| 531 | } |
| 532 | }; |
| 533 | } |
| 534 | if (serr) { |
| 535 | m_State = HTTP_STATE_CANT_CONNECT; |
| 536 | return 0; |
| 537 | } |
| 538 | m_State = HTTP_STATE_CONNECTED; |
| 539 | return 1; |
| 540 | } |
| 541 | |
| 542 | char *ChttpGet::GetHTTPLine() { |
| 543 | uint32_t iBytesRead; |
| 544 | char chunk[2]; |
| 545 | uint32_t igotcrlf = 0; |
| 546 | memset(recv_buffer, 0, 1000); |
| 547 | do { |
| 548 | chunk[0] = '\0'; |
| 549 | bool gotdata = false; |
| 550 | do { |
| 551 | iBytesRead = recv(m_DataSock, chunk, 1, 0); |
| 552 | |
| 553 | if (SOCKET_ERROR == iBytesRead) { |
| 554 | int error = WSAGetLastError(); |
| 555 | #if defined(POSIX) |
| 556 | if (WSAEWOULDBLOCK == error || 0 == error) |
| 557 | #else |
| 558 | if (WSAEWOULDBLOCK == error) |
| 559 | #endif |
| 560 | { |
| 561 | gotdata = false; |
| 562 | continue; |
| 563 | } else { |
| 564 | return NULL; |
| 565 | } |
| 566 | } else { |
| 567 | gotdata = true; |
| 568 | } |
| 569 | } while (!gotdata); |
| 570 | |
| 571 | if (chunk[0] == 0x0d) { |
| 572 | // This should always read a 0x0a |
| 573 | do { |
| 574 | iBytesRead = recv(m_DataSock, chunk, 1, 0); |
| 575 | |
| 576 | if (SOCKET_ERROR == iBytesRead) { |
| 577 | int error = WSAGetLastError(); |
| 578 | #if defined(POSIX) |
| 579 | if (WSAEWOULDBLOCK == error || 0 == error) |
| 580 | #else |
| 581 | if (WSAEWOULDBLOCK == error) |
| 582 | #endif |
| 583 | { |
| 584 | gotdata = false; |
| 585 | continue; |
| 586 | } else { |
| 587 | return NULL; |
| 588 | } |
| 589 | } else { |
| 590 | gotdata = true; |
| 591 | } |
| 592 | } while (!gotdata); |
| 593 | igotcrlf = 1; |
| 594 | } else { |
| 595 | chunk[1] = '\0'; |
| 596 | strcat(recv_buffer, chunk); |
| 597 | } |
| 598 | |
| 599 | } while (igotcrlf == 0); |
| 600 | return recv_buffer; |
| 601 | } |
| 602 | |
| 603 | uint32_t ChttpGet::ReadDataChannel() { |
| 604 | char sDataBuffer[4096]; // Data-storage buffer for the data channel |
| 605 | int nBytesRecv = 0; // Bytes received from the data channel |
| 606 | |
| 607 | fd_set wfds; |
| 608 | |
| 609 | timeval timeout; |
| 610 | timeout.tv_sec = 0; |
| 611 | timeout.tv_usec = 500; |
| 612 | |
| 613 | m_State = HTTP_STATE_RECEIVING; |
| 614 | do { |
| 615 | FD_ZERO(&wfds); |
| 616 | FD_SET(m_DataSock, &wfds); |
| 617 | |
| 618 | if ((m_iBytesTotal) && (m_iBytesIn == m_iBytesTotal)) { |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | select(m_DataSock + 1, &wfds, NULL, NULL, &timeout); |
| 623 | |
| 624 | if (m_Aborting) { |
| 625 | fclose(LOCALFILE); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | nBytesRecv = recv(m_DataSock, (char *)&sDataBuffer, sizeof(sDataBuffer), 0); |
| 630 | |
| 631 | if (m_Aborting) { |
| 632 | fclose(LOCALFILE); |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | if (SOCKET_ERROR == nBytesRecv) { |
| 637 | int error = WSAGetLastError(); |
| 638 | #if defined(POSIX) |
| 639 | if (WSAEWOULDBLOCK == error || 0 == error) |
| 640 | #else |
| 641 | if (WSAEWOULDBLOCK == error) |
| 642 | #endif |
| 643 | { |
| 644 | nBytesRecv = 1; |
| 645 | continue; |
| 646 | } |
| 647 | } |
| 648 | m_iBytesIn += nBytesRecv; |
| 649 | if (nBytesRecv > 0) { |
| 650 | fwrite(sDataBuffer, nBytesRecv, 1, LOCALFILE); |
| 651 | // Write sDataBuffer, nBytesRecv |
| 652 | } |
| 653 | |
| 654 | } while (nBytesRecv > 0); |
| 655 | |
| 656 | fclose(LOCALFILE); |
| 657 | |
| 658 | // Close the file and check for error returns. |
| 659 | if (nBytesRecv == SOCKET_ERROR) { |
| 660 | // Ok, we got a socket error -- xfer aborted? |
| 661 | m_State = HTTP_STATE_RECV_FAILED; |
| 662 | return 0; |
| 663 | } else { |
| 664 | // OutputDebugString("HTTP File complete!\n"); |
| 665 | // done! |
| 666 | m_State = HTTP_STATE_FILE_RECEIVED; |
| 667 | return 1; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | static async_dns_lookup httpaslu; |
| 672 | static async_dns_lookup *http_lastaslu = NULL; |
| 673 | |
| 674 | int http_Asyncgethostbyname(uint32_t *ip, int command, char *hostname) { |
| 675 | |
| 676 | if (command == NW_AGHBN_LOOKUP) { |
| 677 | if (http_lastaslu) |
| 678 | http_lastaslu->abort = true; |
| 679 | |
| 680 | async_dns_lookup *newaslu; |
| 681 | newaslu = (async_dns_lookup *)mem_malloc(sizeof(async_dns_lookup)); |
| 682 | memset(&newaslu->ip, 0, sizeof(uint32_t)); |
| 683 | newaslu->host = hostname; |
| 684 | newaslu->done = false; |
| 685 | newaslu->error = false; |
| 686 | newaslu->abort = false; |
| 687 | http_lastaslu = newaslu; |
| 688 | httpaslu.done = false; |
| 689 | |
| 690 | #ifdef WIN32 |
| 691 | _beginthread(http_gethostbynameworker, 0, newaslu); |
| 692 | #elif defined(POSIX) |
| 693 | // pthread_t thread; |
| 694 | if (!inet_LoadThreadLib()) { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | // df_pthread_create(&thread,NULL,http_gethostbynameworker,newaslu); |
| 699 | newaslu->threadId = SDL_CreateThread(http_gethostbynameworker, "httpdns", newaslu); |
| 700 | #endif |
| 701 | return 1; |
| 702 | } else if (command == NW_AGHBN_CANCEL) { |
| 703 | if (http_lastaslu) |
| 704 | http_lastaslu->abort = true; |
| 705 | |
| 706 | #if defined(POSIX) |
| 707 | SDL_WaitThread(http_lastaslu->threadId, NULL); |
| 708 | #endif |
| 709 | |
| 710 | http_lastaslu = NULL; |
| 711 | } else if (command == NW_AGHBN_READ) { |
| 712 | if (!http_lastaslu) |
| 713 | return -1; |
| 714 | if (httpaslu.done) { |
| 715 | // free(http_lastaslu); |
| 716 | #if defined(POSIX) |
| 717 | SDL_WaitThread(http_lastaslu->threadId, NULL); |
| 718 | #endif |
| 719 | |
| 720 | http_lastaslu = NULL; |
| 721 | memcpy(ip, &httpaslu.ip, sizeof(uint32_t)); |
| 722 | return 1; |
| 723 | } else if (httpaslu.error) { |
| 724 | #if defined(POSIX) |
| 725 | SDL_WaitThread(http_lastaslu->threadId, NULL); |
| 726 | #endif |
| 727 | |
| 728 | mem_free(http_lastaslu); |
| 729 | http_lastaslu = NULL; |
| 730 | return -1; |
| 731 | } else |
| 732 | return 0; |
| 733 | } |
| 734 | return -2; |
| 735 | } |
| 736 | |
| 737 | // This is the worker thread which does the lookup. |
| 738 | #if !defined(POSIX) |
| 739 | void __cdecl http_gethostbynameworker(void *parm) |
| 740 | #else |
| 741 | int http_gethostbynameworker(void *parm) |
| 742 | #endif |
| 743 | { |
| 744 | #if defined(POSIX) |
| 745 | // df_pthread_detach(df_pthread_self()); |
| 746 | #endif |
| 747 | async_dns_lookup *lookup = (async_dns_lookup *)parm; |
| 748 | HOSTENT *he = gethostbyname(lookup->host); |
| 749 | if (he == NULL) { |
| 750 | lookup->error = true; |
| 751 | #if defined(POSIX) |
| 752 | return 0; |
| 753 | #else |
| 754 | return; |
| 755 | #endif |
| 756 | } else if (!lookup->abort) { |
| 757 | memcpy(&lookup->ip, he->h_addr_list[0], sizeof(uint32_t)); |
| 758 | lookup->done = true; |
| 759 | memcpy(&httpaslu, lookup, sizeof(async_dns_lookup)); |
| 760 | } |
| 761 | mem_free(lookup); |
| 762 | |
| 763 | #if defined(POSIX) |
| 764 | return 0; |
| 765 | #endif |
| 766 | } |