httpserv.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2001 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: httpserv.h - JNL interface for doing HTTP GET/POST serving.
  6. ** License: see jnetlib.h
  7. ** This class just manages the http reply/sending, not where the data
  8. ** comes from, etc.
  9. ** for a mini-web server see webserver.h
  10. */
  11. #ifndef _HTTPSERV_H_
  12. #define _HTTPSERV_H_
  13. #include "connection.h"
  14. #include "headers.h"
  15. #include "nswasabi/ReferenceCounted.h"
  16. class JNL_HTTPServ : public ReferenceCountedBase<JNL_HTTPServ>
  17. {
  18. public:
  19. JNL_HTTPServ(JNL_Connection *con=NULL);
  20. ~JNL_HTTPServ();
  21. int run(); // returns: < 0 on error, 0 on request not read yet, 1 if reading headers, 2 if reply not sent, 3 if reply sent, sending data. 4 on connection closed.
  22. const char *geterrorstr() { return m_errstr;}
  23. // use these when state returned by run() is 2
  24. const char *get_request_file(); // file portion of http request
  25. const char *get_request_parm(const char *parmname); // parameter portion (after ?)
  26. const char *getallheaders() { return recvheaders.GetAllHeaders(); } // double null terminated, null delimited list
  27. const char *getheader(const char *headername);
  28. const char *get_method() { return m_method; };
  29. void set_reply_string(const char *reply_string); // should be HTTP/1.1 OK or the like
  30. void add_reply_header(const char *header); // i.e. "content-size: 12345"
  31. void send_reply() { m_reply_ready=1; } // send reply, state will advance to 3.
  32. ////////// sending data ///////////////
  33. int bytes_inqueue() { if (m_state == 3 || m_state == -1 || m_state ==4) return (int)m_con->send_bytes_in_queue(); else return 0; }
  34. int bytes_cansend() { if (m_state == 3) return (int)m_con->send_bytes_available(); else return 0; }
  35. void write_bytes(char *bytes, int length) { m_con->send(bytes,length); }
  36. void close(int quick) { m_con->close(quick); m_state=4; }
  37. JNL_Connection *get_con() { return m_con; }
  38. void reset(); // prepare for another request on the same connection (HTTP/1.1)
  39. int get_http_version() { return http_ver; }
  40. int get_keep_alive() { return keep_alive; }
  41. protected:
  42. void seterrstr(const char *str) { if (m_errstr) free(m_errstr); m_errstr=_strdup(str); }
  43. int m_reply_ready;
  44. int m_state;
  45. int http_ver;
  46. int keep_alive;
  47. char *m_errstr;
  48. char *m_reply_headers;
  49. char *m_reply_string;
  50. JNL_Headers recvheaders;
  51. char *m_recv_request; // either double-null terminated, or may contain parameters after first null.
  52. char *m_method;
  53. JNL_Connection *m_con;
  54. };
  55. #endif // _HTTPSERV_H_