wac_network_http_receiver.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2007 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: httpget.h - JNL interface for doing HTTP GETs.
  6. ** License: see jnetlib.h
  7. **
  8. ** Usage:
  9. ** 1. Create a WAC_Network_HTTPGet object, optionally specifying a wa::Components::WAC_Network_AsyncDNS
  10. ** object to use (or NULL for none, or WAC_NETWORK_CONNECTION_AUTODNS for auto),
  11. ** and the receive buffer size, and a string specifying proxy (or NULL
  12. ** for none). See note on proxy string below.
  13. ** 2. call addheader() to add whatever headers you want. It is recommended to
  14. ** add at least the following two:
  15. ** addheader("User-Agent:MyApp (Mozilla)");
  16. */// addheader("Accept:*/*");
  17. /* ( the comment weirdness is there so I Can do the star-slash :)
  18. ** 3. Call connect() with the URL you wish to GET (see URL string note below)
  19. ** 4. Call run() once in a while, checking to see if it returns -1
  20. ** (if it does return -1, call geterrorstr() to see what the error is).
  21. ** (if it returns 1, no big deal, the connection has closed).
  22. ** 5. While you're at it, you can call bytes_available() to see if any data
  23. ** from the http stream is available, or getheader() to see if any headers
  24. ** are available, or getreply() to see the HTTP reply, or getallheaders()
  25. ** to get a double null terminated, null delimited list of headers returned.
  26. ** 6. If you want to read from the stream, call get_bytes (which returns how much
  27. ** was actually read).
  28. ** 7. content_length() is a helper function that uses getheader() to check the
  29. ** content-length header.
  30. ** 8. Delete ye' ol' object when done.
  31. **
  32. ** Proxy String:
  33. ** should be in the format of host:port, or user@host:port, or
  34. ** user:password@host:port. if port is not specified, 80 is assumed.
  35. ** URL String:
  36. ** should be in the format of http://user:pass@host:port/requestwhatever
  37. ** note that user, pass, port, and /requestwhatever are all optional :)
  38. ** note that also, http:// is really not important. if you do poo://
  39. ** or even leave out the http:// altogether, it will still work.
  40. */
  41. #ifndef NULLSOFT_WAC_NETWORK_HTTP_RECEIVER_H
  42. #define NULLSOFT_WAC_NETWORK_HTTP_RECEIVER_H
  43. #include <atomic>
  44. #include <QtCore>
  45. #include <zlib.h>
  46. #include "wac_network_connection.h"
  47. #include "wac_network_http_receiver_api.h"
  48. #include "foundation/error.h"
  49. //#define WAC_NETWORK_CONNECTION_AUTODNS API_DNS_AUTODNS
  50. namespace wa
  51. {
  52. namespace Components
  53. {
  54. class WAC_Network_HTTPGet : public api_httpreceiver
  55. {
  56. public:
  57. WAC_Network_HTTPGet( api_dns *p_dns = API_DNS_AUTODNS, size_t p_recvbufsize = PACKET_SIZE, const char *p_proxy = NULL );
  58. ~WAC_Network_HTTPGet();
  59. void open( api_dns *p_dns = API_DNS_AUTODNS, size_t p_recvbufsize = PACKET_SIZE, const char *p_proxy = NULL );
  60. void set_sendbufsize( size_t p_sendbufsize = PACKET_SIZE ); // call if you're going to POST or do any kind of bidirectional communications
  61. int set_recv_buffer_size( size_t p_new_buffer_size );
  62. void addheader( const char *header );
  63. void addheadervalue( const char *header, const char *value );
  64. void connect( const char *url, int ver = 0, const char *requestmethod = "GET" );
  65. int run(); // returns: 0 if all is OK. -1 if error (call geterrorstr()). 1 if connection closed.
  66. int get_status(); // returns 0 if connecting, 1 if reading headers, 2 if reading content, -1 if error.
  67. const char *getallheaders(); // double null terminated, null delimited list
  68. char *getheader( const char *headername );
  69. const char *getreply() { return m_reply; }
  70. int getreplycode(); // returns 0 if none yet, otherwise returns http reply code.
  71. const char *geterrorstr() { return m_errstr; }
  72. size_t bytes_available();
  73. size_t get_bytes( char *buf, size_t len );
  74. size_t peek_bytes( char *buf, size_t len );
  75. uint64_t content_length();
  76. api_connection *get_con() { return m_con; }
  77. void AllowCompression();
  78. void reset_headers();
  79. const char *get_url() { return m_http_url; }
  80. void set_accept_all_reply_codes(); // call this if you want to retrieve content even though a 404 (etc) was returned
  81. void set_persistent();
  82. static void set_proxy( const char *p_proxy );
  83. protected:
  84. static char *get_proxy();
  85. void reinit();
  86. void deinit( bool p_full = true );
  87. void seterrstr( const char *str );
  88. void do_parse_url( const char *url, char **host, unsigned short *port, char **req, char **lp );
  89. void do_encode_mimestr( char *in, char *out );
  90. size_t AddRef();
  91. size_t Release();
  92. api_dns *m_dns = API_DNS_AUTODNS;
  93. WAC_Network_Connection *m_con = NULL;
  94. size_t m_recvbufsize = 0;
  95. int m_http_state;
  96. unsigned short m_http_port;
  97. char *m_http_url;
  98. char *m_http_host;
  99. char *m_http_lpinfo;
  100. char *m_http_request;
  101. char *m_http_content_type;
  102. char *m_http_proxylpinfo = 0;
  103. char *m_http_proxyhost = 0;
  104. unsigned short m_http_proxyport = 0;
  105. char *m_sendheaders = 0;
  106. char *m_recvheaders;
  107. size_t m_recvheaders_size = 0;
  108. char *m_reply;
  109. char *m_errstr;
  110. bool allowCompression = false;
  111. size_t m_sendbufsize = 0;
  112. /* gzip stuff */
  113. z_stream *zlibStream = 0;
  114. bool accept_all_reply_codes = false;
  115. bool persistent = false;
  116. static char *g_proxy;
  117. protected:
  118. RECVS_DISPATCH;
  119. private:
  120. volatile std::atomic<std::size_t> _reference_count = 1;
  121. };
  122. }
  123. }
  124. #endif //!NULLSOFT_WAC_NETWORK_HTTP_RECEIVER_H