wac_network_http_receiver.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2007 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: httpget.cpp - JNL HTTP GET implementation
  6. ** License: see jnetlib.h
  7. */
  8. #include "netinc.h"
  9. #include "util.h"
  10. #include "wac_network_http_receiver.h"
  11. #ifdef USE_SSL
  12. #include "wac_network_ssl_connection.h"
  13. #endif
  14. #include <stdio.h>
  15. #pragma intrinsic(memcpy)
  16. #include "../nu/strsafe.h"
  17. #include "nu/AutoLock.h"
  18. #include "..\WAT\WAT.h"
  19. char *wa::Components::WAC_Network_HTTPGet::g_proxy = 0;
  20. static nu::LockGuard proxy_guard;
  21. char *wa::Components::WAC_Network_HTTPGet::get_proxy()
  22. {
  23. nu::AutoLock auto_lock( proxy_guard );
  24. if ( g_proxy )
  25. return _strdup( g_proxy );
  26. else
  27. return 0;
  28. }
  29. void wa::Components::WAC_Network_HTTPGet::set_proxy( const char *p_proxy )
  30. {
  31. nu::AutoLock auto_lock( proxy_guard );
  32. free( g_proxy );
  33. if ( p_proxy )
  34. g_proxy = _strdup( p_proxy );
  35. else
  36. g_proxy = 0;
  37. }
  38. wa::Components::WAC_Network_HTTPGet::WAC_Network_HTTPGet( api_dns *p_dns, size_t p_recvbufsize, const char *p_proxy )
  39. {
  40. JNL::open_socketlib();
  41. reinit();
  42. open( p_dns, p_recvbufsize, p_proxy );
  43. }
  44. wa::Components::WAC_Network_HTTPGet::~WAC_Network_HTTPGet()
  45. {
  46. deinit();
  47. free( m_sendheaders );
  48. free( m_http_proxylpinfo );
  49. free( m_http_proxyhost );
  50. }
  51. void wa::Components::WAC_Network_HTTPGet::open( api_dns *p_dns, size_t p_recvbufsize, const char *p_proxy )
  52. {
  53. m_recvbufsize = p_recvbufsize;
  54. if ( p_dns != API_DNS_AUTODNS )
  55. m_dns = p_dns;
  56. if ( p_proxy )
  57. {
  58. char *p = _strdup( p_proxy );
  59. if ( p )
  60. {
  61. char *r = NULL;
  62. do_parse_url( p, &m_http_proxyhost, &m_http_proxyport, &r, &m_http_proxylpinfo );
  63. free( r );
  64. free( p );
  65. }
  66. }
  67. }
  68. void wa::Components::WAC_Network_HTTPGet::reinit()
  69. {
  70. m_errstr = 0;
  71. m_recvheaders = NULL;
  72. m_recvheaders_size = 0;
  73. m_http_state = 0;
  74. m_http_port = 0;
  75. m_http_url = 0;
  76. m_reply = 0;
  77. m_http_host = NULL;
  78. m_http_lpinfo = NULL;
  79. m_http_request = NULL;
  80. m_http_content_type = NULL;
  81. }
  82. void wa::Components::WAC_Network_HTTPGet::deinit( bool p_full )
  83. {
  84. if ( !persistent || p_full || ( m_con && m_con->get_state() == WAC_Network_Connection::STATE_ERROR ) )
  85. {
  86. delete m_con;
  87. m_con = NULL;
  88. }
  89. free( m_recvheaders );
  90. free( m_http_url );
  91. free( m_http_host );
  92. free( m_http_lpinfo );
  93. free( m_http_request );
  94. if ( m_http_content_type )
  95. free( m_http_content_type );
  96. free( m_errstr );
  97. free( m_reply );
  98. if ( zlibStream )
  99. inflateEnd( zlibStream );
  100. free( zlibStream );
  101. zlibStream = 0;
  102. reinit();
  103. }
  104. void wa::Components::WAC_Network_HTTPGet::set_sendbufsize( size_t p_sendbufsize )
  105. {
  106. m_sendbufsize = p_sendbufsize;
  107. }
  108. int wa::Components::WAC_Network_HTTPGet::set_recv_buffer_size( size_t p_new_buffer_size )
  109. {
  110. if ( m_con )
  111. {
  112. int ret = m_con->set_recv_buffer_size( p_new_buffer_size );
  113. if ( ret == NErr_NoAction )// this will get returned if new_buffer_size is smaller than existing.
  114. return NErr_Success;
  115. else if ( ret != NErr_Success )
  116. return ret;
  117. }
  118. m_recvbufsize = p_new_buffer_size;
  119. return NErr_Success;
  120. }
  121. void wa::Components::WAC_Network_HTTPGet::addheader( const char *header )
  122. {
  123. if ( strstr( header, "\r" ) || strstr( header, "\n" ) )
  124. return;
  125. if ( !m_sendheaders )
  126. {
  127. size_t len = strlen( header ) + 3;
  128. m_sendheaders = (char *)malloc( len );
  129. if ( m_sendheaders )
  130. {
  131. char *itr = m_sendheaders;
  132. StringCchCopyExA( itr, len, header, &itr, &len, 0 );
  133. StringCchCatExA( itr, len, "\r\n", &itr, &len, 0 );
  134. }
  135. }
  136. else
  137. {
  138. size_t len = strlen( header ) + strlen( m_sendheaders ) + 1 + 2;
  139. char *t = (char *)malloc( len );
  140. if ( t )
  141. {
  142. char *newHeaders = t;
  143. StringCchCopyExA( t, len, m_sendheaders, &t, &len, 0 );
  144. StringCchCatExA( t, len, header, &t, &len, 0 );
  145. StringCchCatExA( t, len, "\r\n", &t, &len, 0 );
  146. free( m_sendheaders );
  147. m_sendheaders = newHeaders;
  148. }
  149. }
  150. }
  151. void wa::Components::WAC_Network_HTTPGet::addheadervalue( const char *header, const char *value )
  152. {
  153. size_t additional = strlen( header ) + 2 + strlen( value ) + 2 + 1;
  154. if ( !m_sendheaders )
  155. {
  156. m_sendheaders = (char *)malloc( additional );
  157. if ( m_sendheaders )
  158. {
  159. char *p = m_sendheaders;
  160. StringCchCopyExA( p, additional, header, &p, &additional, 0 );
  161. StringCchCatExA( p, additional, ": ", &p, &additional, 0 );
  162. StringCchCatExA( p, additional, value, &p, &additional, 0 );
  163. StringCchCatExA( p, additional, "\r\n", &p, &additional, 0 );
  164. }
  165. }
  166. else
  167. {
  168. size_t alloc_len = strlen( m_sendheaders ) + additional;
  169. char *t = (char *)malloc( alloc_len );
  170. if ( t )
  171. {
  172. char *p = t;
  173. StringCchCopyExA( p, alloc_len, m_sendheaders, &p, &alloc_len, 0 );
  174. StringCchCatExA( p, alloc_len, header, &p, &alloc_len, 0 );
  175. StringCchCatExA( p, alloc_len, ": ", &p, &alloc_len, 0 );
  176. StringCchCatExA( p, alloc_len, value, &p, &alloc_len, 0 );
  177. StringCchCatExA( p, alloc_len, "\r\n", &p, &alloc_len, 0 );
  178. free( m_sendheaders );
  179. m_sendheaders = t;
  180. }
  181. }
  182. }
  183. void wa::Components::WAC_Network_HTTPGet::do_encode_mimestr( char *in, char *out )
  184. {
  185. char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  186. int shift = 0;
  187. int accum = 0;
  188. while ( in && *in )
  189. {
  190. if ( *in )
  191. {
  192. accum <<= 8;
  193. shift += 8;
  194. accum |= *in++;
  195. }
  196. while ( shift >= 6 )
  197. {
  198. shift -= 6;
  199. *out++ = alphabet[ ( accum >> shift ) & 0x3F ];
  200. }
  201. }
  202. if ( shift == 4 )
  203. {
  204. *out++ = alphabet[ ( accum & 0xF ) << 2 ];
  205. *out++ = '=';
  206. }
  207. else if ( shift == 2 )
  208. {
  209. *out++ = alphabet[ ( accum & 0x3 ) << 4 ];
  210. *out++ = '=';
  211. *out++ = '=';
  212. }
  213. *out++ = 0;
  214. }
  215. void wa::Components::WAC_Network_HTTPGet::connect( const char *url, int ver, const char *requestmethod )
  216. {
  217. deinit( false );
  218. m_http_url = _strdup( url );
  219. do_parse_url( m_http_url, &m_http_host, &m_http_port, &m_http_request, &m_http_lpinfo );
  220. if ( !m_http_host || !m_http_host[ 0 ] || !m_http_port )
  221. {
  222. m_http_state = -1;
  223. seterrstr( "invalid URL" );
  224. return;
  225. }
  226. size_t sendbufferlen = 0;
  227. if ( !m_http_proxyhost || !m_http_proxyhost[ 0 ] )
  228. sendbufferlen += strlen( requestmethod ) + 1 /* GET */ + strlen( m_http_request ) + 9 /* HTTP/1.0 */ + 2;
  229. else
  230. {
  231. sendbufferlen += strlen( requestmethod ) + 1 /* GET */ + strlen( m_http_url ) + 9 /* HTTP/1.0 */ + 2;
  232. if ( m_http_proxylpinfo && m_http_proxylpinfo[ 0 ] )
  233. sendbufferlen += 58 + strlen( m_http_proxylpinfo ) * 2; // being safe here
  234. }
  235. sendbufferlen += 5 /* Host: */ + strlen( m_http_host ) + 2;
  236. if ( m_http_port != 80 )
  237. sendbufferlen += 6;
  238. if ( m_http_lpinfo && m_http_lpinfo[ 0 ] )
  239. sendbufferlen += 46 + strlen( m_http_lpinfo ) * 2; // being safe here
  240. if ( m_sendheaders )
  241. sendbufferlen += strlen( m_sendheaders );
  242. size_t strLen = sendbufferlen + 1024;
  243. char *str = (char *)calloc( strLen, sizeof( char ) );
  244. char *connectString = str;
  245. if ( !str )
  246. {
  247. seterrstr( "error allocating memory" );
  248. m_http_state = -1;
  249. }
  250. if ( !m_http_proxyhost || !m_http_proxyhost[ 0 ] )
  251. {
  252. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "%s %s HTTP/1.%d\r\n", requestmethod, m_http_request, ver % 10 );
  253. }
  254. else
  255. {
  256. char *myp = NULL;
  257. if ( strncasecmp( m_http_url, "uvox://", 7 ) == 0 )
  258. {
  259. myp = m_http_url + 7;
  260. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "%s http: //%s HTTP/1.%d\r\n", requestmethod, myp, ver % 10 );
  261. }
  262. else if ( strncasecmp( m_http_url, "unsv://", 7 ) == 0 )
  263. {
  264. myp = m_http_url + 7;
  265. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "%s http: //%s HTTP/1.%d\r\n", requestmethod, myp, ver % 10 );
  266. }
  267. else if ( strncasecmp( m_http_url, "uasf://", 7 ) == 0 )
  268. {
  269. myp = m_http_url + 7;
  270. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "%s http: //%s HTTP/1.%d\r\n", requestmethod, myp, ver % 10 );
  271. }
  272. else
  273. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "%s %s HTTP/1.%d\r\n", requestmethod, m_http_url, ver % 10 );
  274. }
  275. if ( ver % 10 == 1 )
  276. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "Connection: close\r\n" );
  277. if ( m_http_port == 80 )
  278. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "Host: %s\r\n", m_http_host );
  279. else
  280. StringCchPrintfExA( str, strLen, &str, &strLen, 0, "Host: %s:%d\r\n", m_http_host, m_http_port );
  281. if ( m_http_lpinfo && m_http_lpinfo[ 0 ] )
  282. {
  283. StringCchCatExA( str, strLen, "Authorization: Basic ", &str, &strLen, 0 );
  284. do_encode_mimestr( m_http_lpinfo, str );
  285. StringCchCatExA( str, strLen, "\r\n", &str, &strLen, 0 );
  286. }
  287. if ( m_http_proxylpinfo && m_http_proxylpinfo[ 0 ] )
  288. {
  289. StringCchCatExA( str, strLen, "Proxy-Authorization: Basic ", &str, &strLen, 0 );
  290. do_encode_mimestr( m_http_proxylpinfo, str );
  291. StringCchCatExA( str, strLen, "\r\n", &str, &strLen, 0 );
  292. }
  293. if ( allowCompression )
  294. StringCchCatExA( str, strLen, "Accept-Encoding: compress, gzip\r\n", &str, &strLen, 0 );
  295. if ( m_sendheaders )
  296. StringCchCatExA( str, strLen, m_sendheaders, &str, &strLen, 0 );
  297. StringCchCatExA( str, strLen, "\r\n", &str, &strLen, 0 );
  298. int a = (int)m_recvbufsize;
  299. if ( a < 4096 )
  300. a = 4096;
  301. if ( !m_con )
  302. {
  303. //m_con=new WAC_Network_Connection(m_dns,strlen(str)+4,a);
  304. /*
  305. ** Joshua Teitelbaum delta 1/15/2006
  306. */
  307. #ifdef USE_SSL
  308. /*
  309. ** Joshua Teitelbaum 1/27/2006
  310. ** Check for secure
  311. */
  312. if ( !_strnicmp( m_http_url, "https:", strlen( "https:" ) ) )
  313. {
  314. size_t send_buffer_size = strlen( connectString ) + 4;
  315. if ( send_buffer_size < 8192 )
  316. send_buffer_size = 8192;
  317. send_buffer_size += m_sendbufsize;
  318. if ( m_sendbufsize == 0 && !strcmp( requestmethod, "POST" ) )
  319. send_buffer_size += 8192; // some extra room for posting data if it wasn't explicitly defined
  320. m_con = new JNL_SSL_Connection( NULL, m_dns, send_buffer_size, a );
  321. }
  322. else
  323. {
  324. #endif
  325. size_t send_buffer_size = strlen( connectString ) + 4 + m_sendbufsize;
  326. if ( m_sendbufsize == 0 && !strcmp( requestmethod, "POST" ) )
  327. send_buffer_size += 8192; // some extra room for posting data if it wasn't explicitly defined
  328. m_con = new WAC_Network_Connection( m_dns, send_buffer_size, a );
  329. #ifdef USE_SSL
  330. }
  331. #endif
  332. if ( m_con )
  333. {
  334. if ( !m_http_proxyhost || !m_http_proxyhost[ 0 ] )
  335. m_con->connect( m_http_host, m_http_port );
  336. else
  337. m_con->connect( m_http_proxyhost, m_http_proxyport );
  338. m_con->send_string( connectString );
  339. }
  340. else
  341. {
  342. m_http_state = -1;
  343. seterrstr( "could not create connection object" );
  344. }
  345. }
  346. else
  347. {
  348. m_con->reuse();
  349. m_con->send_string( connectString );
  350. }
  351. free( connectString );
  352. }
  353. void wa::Components::WAC_Network_HTTPGet::do_parse_url( const char *url, char **host, unsigned short *port, char **req, char **lp )
  354. {
  355. char *l_port = 0;
  356. JNL::parse_url( url, &l_port, host, port, req, lp );
  357. if ( !*port )
  358. {
  359. if ( l_port )
  360. {
  361. addrinfo *res;
  362. addrinfo hints;
  363. memset( &hints, 0, sizeof( hints ) );
  364. hints.ai_family = PF_UNSPEC;
  365. hints.ai_flags = 0;
  366. hints.ai_socktype = SOCK_STREAM;
  367. if ( getaddrinfo( 0, l_port, &hints, &res ) == 0 )
  368. {
  369. if ( res->ai_family == AF_INET )
  370. *port = htons( ( (sockaddr_in *)res->ai_addr )->sin_port );
  371. else if ( res->ai_family == AF_INET6 )
  372. *port = htons( ( (sockaddr_in6 *)res->ai_addr )->sin6_port );
  373. else // wtf?
  374. *port = 80;
  375. }
  376. else
  377. *port = 80;
  378. }
  379. else
  380. *port = 80;
  381. }
  382. if ( l_port )
  383. free( l_port );
  384. if ( !*req )
  385. *req = _strdup( "/" );
  386. }
  387. const char *wa::Components::WAC_Network_HTTPGet::getallheaders()
  388. {
  389. // double null terminated, null delimited list
  390. if ( m_recvheaders )
  391. return m_recvheaders;
  392. else
  393. return "\0\0";
  394. }
  395. char *wa::Components::WAC_Network_HTTPGet::getheader( const char *headername )
  396. {
  397. char *ret = NULL;
  398. if ( headername[ 0 ] == 0 || !m_recvheaders )
  399. return NULL;
  400. size_t headername_size = strlen( headername );
  401. char *buf = (char *)malloc( headername_size + 2 );
  402. #ifdef _WIN32
  403. StringCchCopyA( buf, headername_size + 2, headername );
  404. #elif defined(__APPLE__)
  405. strlcpy( buf, headername, headername_size + 2 );
  406. #else
  407. strncpy( buf, headername, headername_size + 1 );
  408. buf[ headername_size + 1 ] = 0;
  409. #endif
  410. if ( buf[ headername_size - 1 ] != ':' )
  411. {
  412. buf[ headername_size++ ] = ':';
  413. buf[ headername_size ] = 0;
  414. }
  415. char *p = m_recvheaders;
  416. while ( p && *p )
  417. {
  418. if ( !strncasecmp( buf, p, headername_size ) )
  419. {
  420. ret = p + headername_size;
  421. while ( ret && *ret && *ret == ' ' )
  422. ret++;
  423. break;
  424. }
  425. p += strlen( p ) + 1;
  426. }
  427. free( buf );
  428. return ret;
  429. }
  430. int wa::Components::WAC_Network_HTTPGet::run()
  431. {
  432. int cnt = 0;
  433. if ( m_http_state == -1 || !m_con )
  434. return HTTPRECEIVER_RUN_ERROR; // error
  435. run_again:
  436. m_con->run();
  437. if ( m_con->get_state() == WAC_Network_Connection::STATE_ERROR )
  438. {
  439. seterrstr( m_con->get_errstr() );
  440. return HTTPRECEIVER_RUN_ERROR;
  441. }
  442. if ( m_con->get_state() == WAC_Network_Connection::STATE_CLOSED )
  443. return HTTPRECEIVER_RUN_CONNECTION_CLOSED;
  444. if ( m_http_state == 0 ) // connected, waiting for reply
  445. {
  446. if ( m_con->recv_lines_available() > 0 )
  447. {
  448. char buf[ 4096 ] = { 0 };
  449. m_con->recv_line( buf, 4096 );
  450. buf[ 4095 ] = 0;
  451. if ( m_reply && getreplycode() == 100 )
  452. {
  453. free( m_reply );
  454. m_reply = 0;
  455. goto run_again;
  456. }
  457. m_reply = _strdup( buf );
  458. int code = getreplycode();
  459. if ( code >= 200 && code <= 206 )
  460. m_http_state = 2; // proceed to read headers normally
  461. else if ( code == 301 || code == 302 || code == 303 || code == 307 )
  462. {
  463. m_http_state = 1; // redirect city
  464. }
  465. else if ( code != 100 ) // in case of HTTP 100 Continue code, we'll keep looping
  466. {
  467. if ( accept_all_reply_codes )
  468. {
  469. m_http_state = 2; // proceed to read headers normally
  470. }
  471. else
  472. {
  473. seterrstr( buf );
  474. m_http_state = -1;
  475. return HTTPRECEIVER_RUN_ERROR;
  476. }
  477. }
  478. cnt = 0;
  479. }
  480. else if ( !cnt++ )
  481. goto run_again;
  482. }
  483. if ( m_http_state == 1 ) // redirect
  484. {
  485. char *loc = 0;
  486. while ( m_con->recv_lines_available() > 0 )
  487. {
  488. char buf[ 4096 ] = { 0 };
  489. m_con->recv_line( buf, 4096 );
  490. buf[ 4095 ] = 0;
  491. if ( !buf[ 0 ] )
  492. {
  493. if ( !loc )
  494. {
  495. m_http_state = -1;
  496. return HTTPRECEIVER_RUN_ERROR;
  497. }
  498. else
  499. break;
  500. }
  501. if ( !strncasecmp( buf, "Location:", 9 ) )
  502. {
  503. char *p = buf + 9;
  504. while ( p && *p && *p == ' ' ) p++;
  505. if ( p && *p )
  506. {
  507. // TODO need to make this match the request type
  508. loc = _strdup( p );
  509. }
  510. }
  511. }
  512. if ( loc )
  513. {
  514. connect( loc, 1 );
  515. free( loc );
  516. return HTTPRECEIVER_RUN_OK;
  517. }
  518. }
  519. /* ----- read headers ----- */
  520. if ( m_http_state == 2 )
  521. {
  522. if ( !cnt++ && m_con->recv_lines_available() < 1 )
  523. goto run_again;
  524. while ( m_con->recv_lines_available() > 0 )
  525. {
  526. char buf[ 8192 ] = { 0 };
  527. m_con->recv_line( buf, 8192 );
  528. buf[ 8191 ] = 0;
  529. if ( !buf[ 0 ] )
  530. {
  531. const char *compression = getheader( "Content-Encoding" );
  532. if ( compression && !strcmp( compression, "gzip" ) )
  533. {
  534. zlibStream = (z_stream *)malloc( sizeof( z_stream ) );
  535. zlibStream->next_in = Z_NULL;
  536. zlibStream->avail_in = Z_NULL;
  537. zlibStream->next_out = Z_NULL;
  538. zlibStream->avail_out = Z_NULL;
  539. zlibStream->zalloc = (alloc_func)0;
  540. zlibStream->zfree = (free_func)0;
  541. zlibStream->opaque = 0;
  542. int z_err = inflateInit2( zlibStream, 15 + 16 /* +16 for gzip */ );
  543. if ( z_err != Z_OK )
  544. {
  545. free( zlibStream );
  546. zlibStream = 0;
  547. }
  548. }
  549. else
  550. {
  551. if ( zlibStream )
  552. {
  553. free( zlibStream );
  554. zlibStream = 0;
  555. }
  556. }
  557. m_http_state = 3;
  558. break;
  559. }
  560. if ( !m_recvheaders )
  561. {
  562. m_recvheaders_size = strlen( buf ) + 1;
  563. if ( m_recvheaders_size == 0 || m_recvheaders_size == (size_t)-1 ) // check for overflow
  564. {
  565. m_http_state = -1;
  566. return HTTPRECEIVER_RUN_ERROR;
  567. }
  568. m_recvheaders = (char *)malloc( m_recvheaders_size + 1 );
  569. if ( m_recvheaders )
  570. {
  571. strcpy( m_recvheaders, buf ); // safe because we malloc'd specifically above
  572. m_recvheaders[ m_recvheaders_size ] = 0;
  573. }
  574. else
  575. {
  576. m_http_state = -1;
  577. return HTTPRECEIVER_RUN_ERROR;
  578. }
  579. }
  580. else
  581. {
  582. size_t oldsize = m_recvheaders_size;
  583. m_recvheaders_size += strlen( buf ) + 1;
  584. if ( m_recvheaders_size + 1 < oldsize ) // check for overflow
  585. {
  586. m_http_state = -1;
  587. return HTTPRECEIVER_RUN_ERROR;
  588. }
  589. char *n = (char *)realloc( m_recvheaders, m_recvheaders_size + 1 );
  590. if ( !n )
  591. {
  592. m_http_state = -1;
  593. return HTTPRECEIVER_RUN_ERROR;
  594. }
  595. strcpy( n + oldsize, buf ); // safe because we malloc specifially for the size
  596. n[ m_recvheaders_size ] = 0; // double null terminate
  597. m_recvheaders = n;
  598. }
  599. }
  600. }
  601. return HTTPRECEIVER_RUN_OK;
  602. }
  603. int wa::Components::WAC_Network_HTTPGet::get_status() // returns 0 if connecting, 1 if reading headers, 2 if reading content, -1 if error.
  604. {
  605. if ( m_http_state < 0 )
  606. return HTTPRECEIVER_STATUS_ERROR;
  607. if ( m_http_state < 2 )
  608. return HTTPRECEIVER_STATUS_CONNECTING;
  609. if ( m_http_state == 2 )
  610. return HTTPRECEIVER_STATUS_READING_HEADERS;
  611. if ( m_http_state == 3 )
  612. return HTTPRECEIVER_STATUS_READING_CONTENT;
  613. return HTTPRECEIVER_STATUS_ERROR;
  614. }
  615. int wa::Components::WAC_Network_HTTPGet::getreplycode() // returns 0 if none yet, otherwise returns http reply code.
  616. {
  617. if ( !m_reply )
  618. return 0;
  619. char *p = m_reply;
  620. while ( p && *p && *p != ' ' )
  621. p++; // skip over HTTP/x.x
  622. if ( !p || !*p )
  623. return 0;
  624. return atoi( ++p );
  625. }
  626. size_t wa::Components::WAC_Network_HTTPGet::bytes_available()
  627. {
  628. if ( m_con && m_http_state == 3 )
  629. return m_con->recv_bytes_available();
  630. return 0;
  631. }
  632. size_t wa::Components::WAC_Network_HTTPGet::get_bytes( char *buf, size_t len )
  633. {
  634. if ( m_con && m_http_state == 3 )
  635. {
  636. if ( zlibStream )
  637. {
  638. // TODO: benski> we need to pick a better buffer size
  639. // either alloca() and use the passed in length
  640. // or malloc a buffer based on the constructor-initted buffer size
  641. char temp[ 8192 ] = { 0 };
  642. int size = (int)m_con->peek_bytes( temp, 8192 );
  643. if ( size )
  644. {
  645. zlibStream->next_in = reinterpret_cast<Bytef *>( temp );
  646. zlibStream->avail_in = (uInt)size;
  647. zlibStream->next_out = reinterpret_cast<Bytef *>( buf );
  648. zlibStream->avail_out = (uInt)len;
  649. int zlib_err = inflate( zlibStream, Z_SYNC_FLUSH );
  650. if ( zlib_err == Z_OK || zlib_err == Z_STREAM_END )
  651. {
  652. m_con->recv_bytes( 0, size - zlibStream->avail_in ); // since we only peeked above
  653. return len - zlibStream->avail_out;
  654. }
  655. else
  656. return 0; // TODO: should we do something else here?
  657. }
  658. }
  659. else
  660. return m_con->recv_bytes( buf, len );
  661. }
  662. return 0;
  663. }
  664. size_t wa::Components::WAC_Network_HTTPGet::peek_bytes( char *buf, size_t len )
  665. {
  666. if ( m_con && m_http_state == 3 )
  667. {
  668. if ( zlibStream )
  669. return 0; // TODO: benski> how are we going to do peek_bytes, since the inflater saves state?
  670. else
  671. return m_con->peek_bytes( buf, len );
  672. }
  673. return 0;
  674. }
  675. uint64_t wa::Components::WAC_Network_HTTPGet::content_length()
  676. {
  677. const char *p = getheader( "Content-Length" );
  678. if ( p && *p )
  679. return strtoull( p, 0, 10 );
  680. else
  681. {
  682. // TODO need to check this further for reliability!
  683. // Helps to handle responses without content-length
  684. if ( m_recvheaders_size > 0 && bytes_available() > 0 )
  685. return bytes_available() - m_recvheaders_size;
  686. }
  687. return 0;
  688. }
  689. void wa::Components::WAC_Network_HTTPGet::seterrstr( const char *str )
  690. {
  691. if ( m_errstr )
  692. free( m_errstr );
  693. m_errstr = _strdup( str );
  694. }
  695. void wa::Components::WAC_Network_HTTPGet::AllowCompression()
  696. {
  697. allowCompression = true;
  698. }
  699. void wa::Components::WAC_Network_HTTPGet::reset_headers()
  700. {
  701. if ( m_sendheaders )
  702. {
  703. free( m_sendheaders );
  704. m_sendheaders = 0;
  705. }
  706. }
  707. void wa::Components::WAC_Network_HTTPGet::set_accept_all_reply_codes()
  708. {
  709. accept_all_reply_codes = true;
  710. }
  711. void wa::Components::WAC_Network_HTTPGet::set_persistent()
  712. {
  713. persistent = true;
  714. }
  715. size_t wa::Components::WAC_Network_HTTPGet::AddRef()
  716. {
  717. return this->_reference_count.fetch_add( 1 );
  718. }
  719. size_t wa::Components::WAC_Network_HTTPGet::Release()
  720. {
  721. std::size_t l_reference_count = this->_reference_count.fetch_sub( 1 );
  722. if ( l_reference_count == 0 )
  723. delete this;
  724. return l_reference_count;
  725. }
  726. #ifdef CBCLASS
  727. #undef CBCLASS
  728. #endif
  729. #define CBCLASS wa::Components::WAC_Network_HTTPGet
  730. START_DISPATCH;
  731. CB( ADDREF, AddRef )
  732. CB( RELEASE, Release )
  733. VCB( API_HTTPRECEIVER_OPEN, open )
  734. VCB( API_HTTPRECEIVER_ADDHEADER, addheader )
  735. VCB( API_HTTPRECEIVER_ADDHEADERVALUE, addheadervalue )
  736. VCB( API_HTTPRECEIVER_CONNECT, connect )
  737. CB( API_HTTPRECEIVER_RUN, run )
  738. CB( API_HTTPRECEIVER_GETSTATUS, get_status )
  739. CB( API_HTTPRECEIVER_GETBYTESAVAILABLE, bytes_available )
  740. CB( API_HTTPRECEIVER_GETBYTES, get_bytes )
  741. CB( API_HTTPRECEIVER_PEEKBYTES, peek_bytes )
  742. CB( API_HTTPRECEIVER_GETHEADER, getheader )
  743. CB( API_HTTPRECEIVER_GETCONTENTLENGTH, content_length )
  744. CB( API_HTTPRECEIVER_GETALLHEADERS, getallheaders )
  745. CB( API_HTTPRECEIVER_GETREPLYCODE, getreplycode )
  746. CB( API_HTTPRECEIVER_GETREPLY, getreply )
  747. CB( API_HTTPRECEIVER_GETERROR, geterrorstr )
  748. CB( API_HTTPRECEIVER_GETCONNECTION, get_con )
  749. VCB( API_HTTPRECEIVER_ALLOW_COMPRESSION, AllowCompression )
  750. VCB( API_HTTPRECEIVER_RESET_HEADERS, reset_headers )
  751. CB( API_HTTPRECEIVER_GET_URL, get_url )
  752. VCB( API_HTTPRECEIVER_SET_SENDBUFSIZE, set_sendbufsize )
  753. VCB( API_HTTPRECEIVER_SET_ACCEPT_ALL_REPLY_CODES, set_accept_all_reply_codes )
  754. VCB( API_HTTPRECEIVER_SET_PERSISTENT, set_persistent )
  755. END_DISPATCH;
  756. #undef CBCLASS