home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / NSISdl / httpget.h < prev    next >
C/C++ Source or Header  |  2002-08-02  |  4KB  |  110 lines

  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2001 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 JNL_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.  
  42. #ifndef _HTTPGET_H_
  43. #define _HTTPGET_H_
  44.  
  45. #include "connection.h"
  46.  
  47. class JNL_HTTPGet
  48. {
  49.   public:
  50.     JNL_HTTPGet(JNL_AsyncDNS *dns=JNL_CONNECTION_AUTODNS, int recvbufsize=16384, char *proxy=NULL);
  51.     ~JNL_HTTPGet();
  52.  
  53.     void addheader(char *header);
  54.  
  55.     void connect(char *url);
  56.  
  57.     int run(); // returns: 0 if all is OK. -1 if error (call geterrorstr()). 1 if connection closed.
  58.  
  59.     int   get_status(); // returns 0 if connecting, 1 if reading headers, 
  60.                         // 2 if reading content, -1 if error.
  61.  
  62.     char *getallheaders(); // double null terminated, null delimited list
  63.     char *getheader(char *headername);
  64.     char *getreply() { return m_reply; }
  65.     int   getreplycode(); // returns 0 if none yet, otherwise returns http reply code.
  66.  
  67.     char *geterrorstr() { return m_errstr;}
  68.  
  69.     int bytes_available();
  70.     int get_bytes(char *buf, int len);
  71.     int peek_bytes(char *buf, int len);
  72.  
  73.     int content_length() { char *p=getheader("content-length:"); if (p) return my_atoi(p); return 0; }
  74.  
  75.     JNL_Connection *get_con() { return m_con; }
  76.  
  77.   public:
  78.     void reinit();
  79.     void deinit();
  80.     void seterrstr(char *str) { if (m_errstr) free(m_errstr); m_errstr=(char*)malloc(strlen(str)+1); strcpy(m_errstr,str); }
  81.  
  82.     void do_parse_url(char *url, char **host, int *port, char **req, char **lp);
  83.     void do_encode_mimestr(char *in, char *out);
  84.  
  85.     JNL_AsyncDNS *m_dns;
  86.     JNL_Connection *m_con;
  87.     int m_recvbufsize;
  88.  
  89.     int m_http_state;
  90.  
  91.     int m_http_port;
  92.     char *m_http_url;
  93.     char *m_http_host;
  94.     char *m_http_lpinfo;
  95.     char *m_http_request;
  96.  
  97.     char *m_http_proxylpinfo;
  98.     char *m_http_proxyhost;
  99.     int   m_http_proxyport;
  100.  
  101.     char *m_sendheaders;
  102.     char *m_recvheaders;
  103.     int m_recvheaders_size;
  104.     char *m_reply;
  105.  
  106.     char *m_errstr;
  107. };
  108.  
  109. #endif // _HTTPGET_H_
  110.