httpget.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 JNL_HTTPGet object, optionally specifying a JNL_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 _HTTPGET_H_
  42. #define _HTTPGET_H_
  43. #include "connection.h"
  44. #include "asyncdns.h"
  45. #include "jnetlib_defines.h"
  46. #ifdef _WIN32
  47. #include "minizip/unzip.h"
  48. #else
  49. #include <zlib.h>
  50. #endif
  51. #include "nswasabi/ReferenceCounted.h"
  52. class JNL_HTTPGet : public ReferenceCountedBase<JNL_HTTPGet>
  53. {
  54. public:
  55. JNL_HTTPGet(size_t recvbufsize=PACKET_SIZE, size_t sendbufsize=0);
  56. ~JNL_HTTPGet();
  57. void set_sendbufsize( size_t sendbufsize = PACKET_SIZE ); // call if you're going to POST or do any kind of bidirectional communications
  58. int set_recv_buffer_size(size_t new_buffer_size);
  59. void addheader(const char *header);
  60. void addheadervalue(const char *header, const char *value);
  61. void connect(const char *url, int ver=0, const char *requestmethod="GET");
  62. int run(); // returns: 0 if all is OK. -1 if error (call geterrorstr()). 1 if connection closed.
  63. int get_status(); // returns 0 if connecting, 1 if reading headers,
  64. // 2 if reading content, -1 if error.
  65. const char *getallheaders(); // double null terminated, null delimited list
  66. const char *getheader(const char *headername);
  67. const char *getreply() { return m_reply; }
  68. int getreplycode(); // returns 0 if none yet, otherwise returns http reply code.
  69. const char *geterrorstr() { return m_errstr;}
  70. size_t bytes_available();
  71. size_t get_bytes(char *buf, size_t len);
  72. size_t peek_bytes(char *buf, size_t len);
  73. uint64_t content_length();
  74. JNL_Connection *get_con() { return m_con; }
  75. void AllowCompression();
  76. void reset_headers();
  77. void set_dns(JNL_AsyncDNS *dns);
  78. const char *get_url() { return m_http_url; }
  79. void set_accept_all_reply_codes(); // call this if you want to retrieve content even though a 404 (etc) was returned
  80. void set_persistent();
  81. static void set_proxy(const char *proxy);
  82. protected:
  83. static char *get_proxy();
  84. void reinit();
  85. void deinit(bool full=true);
  86. void seterrstr(const char *str);
  87. void do_parse_url(const char *url, char **host, unsigned short*port, char **req, char **lp);
  88. void do_encode_mimestr(char *in, char *out);
  89. JNL_AsyncDNS *m_dns;
  90. JNL_Connection *m_con;
  91. size_t m_recvbufsize;
  92. int m_http_state;
  93. unsigned short m_http_port;
  94. char *m_http_url;
  95. char *m_http_host;
  96. char *m_http_lpinfo;
  97. char *m_http_request;
  98. char *m_http_proxylpinfo;
  99. char *m_http_proxyhost;
  100. unsigned short m_http_proxyport;
  101. char *m_sendheaders;
  102. char *m_recvheaders;
  103. size_t m_recvheaders_size;
  104. char *m_reply;
  105. char *m_errstr;
  106. bool allowCompression;
  107. size_t m_sendbufsize;
  108. /* gzip stuff */
  109. z_stream *zlibStream;
  110. bool accept_all_reply_codes;
  111. bool persistent;
  112. static char *g_proxy;
  113. };
  114. #endif // _HTTPGET_H_