wac_network_web_server.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2003 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: wac_network_web_server.h - Generic simple webserver baseclass
  6. ** License: see jnetlib.h
  7. **
  8. ** You can derive your object from WebServerBaseClass to do simple web serving. Example:
  9. class wwwServer : public WebServerBaseClass
  10. {
  11. public:
  12. wwwServer() { }
  13. virtual IPageGenerator *onConnection(wa::Components::WAC_Network_HTTP_Server *serv, int port)
  14. {
  15. serv->set_reply_header("Server:jnetlib_test/0.0");
  16. if (!strcmp(serv->get_request_file(),"/"))
  17. {
  18. serv->set_reply_string("HTTP/1.1 200 OK");
  19. serv->set_reply_header("Content-Type:text/html");
  20. serv->send_reply();
  21. return new MemPageGenerator(_strdup("Test Web Server v0.0"));
  22. }
  23. else
  24. {
  25. serv->set_reply_string("HTTP/1.1 404 NOT FOUND");
  26. serv->send_reply();
  27. return 0; // no data
  28. }
  29. }
  30. };
  31. wwwServer foo;
  32. foo.addListenPort(8080);
  33. while (1)
  34. {
  35. foo.run();
  36. Sleep(10);
  37. }
  38. You will also need to derive from the IPageGenerator interface to provide a data stream, here is an
  39. example of MemPageGenerator:
  40. class MemPageGenerator : public IPageGenerator
  41. {
  42. public:
  43. virtual ~MemPageGenerator() { free(m_buf); }
  44. MemPageGenerator(char *buf, int buf_len=-1) { m_buf=buf; if (buf_len >= 0) m_buf_size=buf_len; else m_buf_size=strlen(buf); m_buf_pos=0; }
  45. virtual int GetData(char *buf, int size) // return 0 when done
  46. {
  47. int a=m_buf_size-m_buf_pos;
  48. if (a < size) size=a;
  49. memcpy(buf,m_buf+m_buf_pos,size);
  50. m_buf_pos+=size;
  51. return size;
  52. }
  53. private:
  54. char *m_buf;
  55. int m_buf_size;
  56. int m_buf_pos;
  57. };
  58. **
  59. */
  60. #ifndef NULLSOFT_WAC_NETWORK_WEB_SERVER_H
  61. #define NULLSOFT_WAC_NETWORK_WEB_SERVER_H
  62. #include "netinc.h"
  63. #include "wac_network.h"
  64. #include "wac_network_web_server_api.h"
  65. #include "wac_network_page_generator_api.h"
  66. class WS_ItemList;
  67. class WS_conInst;
  68. class WebServerBaseClass : public api_webserv
  69. {
  70. public:
  71. WebServerBaseClass();
  72. virtual ~WebServerBaseClass();
  73. void initForFactory(); //calls constructor in factory
  74. void SetConnectionCallback( api_onconncb *_connectionCallback );
  75. void AllowCompression();
  76. // stuff for setting limits/timeouts
  77. void setMaxConnections( int max_con );
  78. void setRequestTimeout( int timeout_s );
  79. // stuff for setting listener port
  80. int addListenPort( int port, unsigned long which_interface = 0 ); // TODO: add Protocol Family as parameter
  81. int getListenPort( int idx, int *err = 0 );
  82. void removeListenPort( int port );
  83. void removeListenIdx( int idx );
  84. // call this a lot :)
  85. void run( void );
  86. // if you want to manually attach a connection, use this:
  87. // you need to specify the port it came in on so the web server can build
  88. // links.
  89. void attachConnection( WAC_Network_Connection *con, JNL_Listen *listener );
  90. // these functions are called for external use through wasabi
  91. // so that static properties will work correctly
  92. void wasabi_url_encode( char *in, char *out, int max_out )
  93. {
  94. url_encode( in, out, max_out );
  95. }
  96. void wasabi_url_decode( char *in, char *out, int maxlen )
  97. {
  98. url_decode( in, out, maxlen );
  99. }
  100. void wasabi_base64decode( char *src, char *dest, int destsize )
  101. {
  102. base64decode( src, dest, destsize );
  103. }
  104. void wasabi_base64encode( char *src, char *dest )
  105. {
  106. base64encode( src, dest );
  107. }
  108. int wasabi_parseAuth( char *auth_header, char *out, int out_len )
  109. {
  110. return parseAuth( auth_header, out, out_len );
  111. }
  112. // stats getting functions
  113. // these can be used externally, as well as are used by the web server
  114. static void url_encode( char *in, char *out, int max_out );
  115. static void url_decode( char *in, char *out, int maxlen );
  116. static void base64decode( char *src, char *dest, int destsize );
  117. static void base64encode( char *in, char *out );
  118. static int parseAuth( char *auth_header, char *out, int out_len );//returns 0 on unknown auth, 1 on basic
  119. protected:
  120. RECVS_DISPATCH;
  121. private:
  122. enum
  123. {
  124. RUN_CONNECTION_CONTINUE = 0,
  125. RUN_CONNECTION_DONE = 1,
  126. RUN_CONNECTION_ERROR = 2,
  127. RUN_CONNECTION_TIMEOUT = 3,
  128. };
  129. int run_connection( WS_conInst *con );
  130. int m_timeout_s;
  131. int m_max_con;
  132. WS_ItemList *m_listeners;
  133. int m_listener_rot;
  134. WS_ItemList *m_connections;
  135. api_onconncb *connectionCallback;
  136. bool allowCompression;
  137. char *extraDeflateBuffer;
  138. int extraDeflateBufferSize;
  139. int extraDeflateBufferUsed;
  140. void deflatehelper( WS_conInst *con, char *buf, int l, bool flush );
  141. };
  142. #endif //!NULLSOFT_WAC_NETWORK_WEB_SERVER_H