wac_network_http_server.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2001 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: wac_network_http_server.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 wac_network_web_server.h
  10. */
  11. #ifndef NULLSOFT_WAC_NETWORK_HTTP_SERVER_H
  12. #define NULLSOFT_WAC_NETWORK_HTTP_SERVER_H
  13. #include "headers.h"
  14. #include "wac_network_http_server_api.h"
  15. #include "wac_network_connection.h"
  16. namespace wa
  17. {
  18. namespace Components
  19. {
  20. class WAC_Network_HTTP_Server : public api_httpserv
  21. {
  22. public:
  23. WAC_Network_HTTP_Server( WAC_Network_Connection *con = NULL );
  24. ~WAC_Network_HTTP_Server();
  25. 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.
  26. const char *geterrorstr() { return m_errstr; }
  27. // use these when state returned by run() is 2
  28. const char *get_request_file(); // file portion of http request
  29. const char *get_request_parm( const char *parmname ); // parameter portion (after ?)
  30. const char *getallheaders() { return recvheaders.GetAllHeaders(); } // double null terminated, null delimited list
  31. const char *getheader( const char *headername );
  32. const char *get_method() { return m_method; }
  33. void set_reply_string( const char *reply_string ); // should be HTTP/1.1 OK or the like
  34. void add_reply_header( const char *header ); // i.e. "content-size: 12345"
  35. void send_reply() { m_reply_ready = 1; } // send reply, state will advance to 3.
  36. ////////// sending data ///////////////
  37. int bytes_inqueue() { if ( m_state == 3 || m_state == -1 || m_state == 4 ) return (int)m_con->send_bytes_in_queue(); else return 0; }
  38. int bytes_cansend() { if ( m_state == 3 ) return (int)m_con->send_bytes_available(); else return 0; }
  39. void write_bytes( char *bytes, int length ) { m_con->send( bytes, length ); }
  40. void close( int quick ) { m_con->close( quick ); m_state = 4; }
  41. api_connection *get_con() { return m_con; }
  42. void reset(); // prepare for another request on the same connection (HTTP/1.1)
  43. int get_http_version() { return http_ver; }
  44. int get_keep_alive() { return keep_alive; }
  45. protected:
  46. void seterrstr( const char *str ) { if ( m_errstr ) free( m_errstr ); m_errstr = _strdup( str ); }
  47. int m_reply_ready;
  48. int m_state;
  49. int http_ver;
  50. int keep_alive;
  51. char *m_errstr;
  52. char *m_reply_headers;
  53. char *m_reply_string;
  54. JNL_Headers recvheaders;
  55. char *m_recv_request; // either double-null terminated, or may contain parameters after first null.
  56. char *m_method;
  57. WAC_Network_Connection *m_con;
  58. RECVS_DISPATCH;
  59. };
  60. }
  61. }
  62. #endif //!NULLSOFT_WAC_NETWORK_HTTP_SERVER_H