/HttpServer.h

https://github.com/privacore/open-source-search-engine · C Header · 187 lines · 90 code · 39 blank · 58 comment · 1 complexity · c81c2a3dfefde877d0519823a4759cb5 MD5 · raw file

  1. // Copyright Matt Wells Nov 2000
  2. // . derived from TcpServer
  3. // . fill in our own getMsgSize () -- looks for Content-Length:xxx
  4. // . fill in our own getMsgPiece() -- looks on disk
  5. // . fill in our own putMsgPiece() -- ??? for spidering big files!
  6. // . all the shit is just a generic non-blocking i/o system
  7. // . move data from one file/mem to another file/mem that might be remote
  8. //
  9. //TODO: handle SIG_PIPEs!! use sigaction() ...
  10. //TODO: first packet should have some file in it, not just MIME hdr (avoid TCP delayed ACKS)
  11. // TODO: what's TCP_CORK??? it delays sending a packet until it's full
  12. // which improves performance quite a bit. unsetting TCP_CORK flushes it.
  13. // TODO: investigate sendfile() (copies data between file descriptors)
  14. #ifndef GB_HTTPSERVER_H
  15. #define GB_HTTPSERVER_H
  16. #define MAX_DOWNLOADS (MAX_TCP_SOCKS-50)
  17. #include "TcpServer.h"
  18. class HttpRequest;
  19. #define DEFAULT_HTTP_PROTO "HTTP/1.0"
  20. typedef void (*tcp_callback_t)(void *, TcpSocket *);
  21. int32_t getMsgSize(const char *buf, int32_t bufSize, TcpSocket *s);
  22. class HttpServer {
  23. public:
  24. // reset the tcp server
  25. void reset();
  26. // returns false if initialization was unsuccessful
  27. bool init( int16_t port, int16_t sslPort, void handlerWrapper( TcpSocket *s ) = NULL );
  28. // . returns false if blocked, true otherwise
  29. // . sets errno on error
  30. // . supports partial gets with "offset" and "size"
  31. // . IMPORTANT: we free read/send bufs of TcpSocket after callback
  32. // . IMPORTANT: if you don't like this set s->m_read/sendBuf to NULL
  33. // in your callback function
  34. // . NOTE: this should always block unless errno is set
  35. // . the TcpSocket's callbackData is a file ptr
  36. // . replies MUST fit in memory (we have NOT implemented putMsgPiece())
  37. // . uses the HTTP partial GET command if size is > 0
  38. // . uses regular GET if size is -1
  39. // . otherwise uses the HTTP HEAD command
  40. // . the document will be in the s->m_readBuf/s->m_bytesRead of "s"
  41. // . use Mime class to help parse the readBuf
  42. // . timeout is in milliseconds since last read OR write
  43. // . this now ensures that the read content is NULL terminated!
  44. bool getDoc ( char *url , // Url *url ,
  45. int32_t ip ,
  46. int32_t offset ,
  47. int32_t size ,
  48. time_t ifModifiedSince ,
  49. void *state ,
  50. void (* callback) ( void *state , TcpSocket *s ) ,
  51. int32_t timeout , // 60*1000
  52. int32_t proxyIp ,
  53. int16_t proxyPort,
  54. int32_t maxTextDocLen ,
  55. int32_t maxOtherDocLen ,
  56. const char *userAgent = NULL ,
  57. // . say HTTP/1.1 instead of 1.0 so we can communicate
  58. // with room alert...
  59. // . we do not support 1.1 that is why you should always
  60. // use 1.0
  61. const char *proto = DEFAULT_HTTP_PROTO , // "HTTP/1.0" ,
  62. bool doPost = false ,
  63. const char *cookieJar = NULL ,
  64. const char *additionalHeader = NULL , // does not include \r\n
  65. // specify your own mime and post data here...
  66. const char *fullRequest = NULL ,
  67. const char *postContent = NULL ,
  68. const char *proxyUsernamePwdAuth = NULL );
  69. bool gotDoc ( int32_t n , TcpSocket *s );
  70. // . this is public so requestHandlerWrapper() can call it
  71. // . if it returns false "s" will be destroyed w/o a reply
  72. void requestHandler ( TcpSocket *s );
  73. // send an error reply, like "HTTP/1.1 404 Not Found"
  74. bool sendErrorReply ( TcpSocket *s, int32_t error, const char *errmsg,
  75. int32_t *bytesSent = NULL );
  76. bool sendErrorReply ( class GigablastRequest *gr );
  77. // xml and json uses this
  78. bool sendSuccessReply ( class GigablastRequest *gr, const char *addMsg=NULL);
  79. bool sendSuccessReply (TcpSocket *s, char format, const char *addMsg=NULL);
  80. // send a "prettier" error reply, formatted in XML if necessary
  81. bool sendQueryErrorReply ( TcpSocket *s , int32_t error , const char *errmsg,
  82. // FORMAT_HTML=0,FORMAT_XML,FORMAT_JSON
  83. char format, int errnum,
  84. const char *content=NULL);
  85. // these are for stopping annoying seo bots
  86. void getKey ( int32_t *key, char *kname,
  87. char *q , int32_t qlen , int32_t now , int32_t s , int32_t n ) ;
  88. void getKeys ( int32_t *key1, int32_t *key2, char *kname1, char *kname2,
  89. char *q , int32_t qlen , int32_t now , int32_t s , int32_t n ) ;
  90. bool hasPermission ( int32_t ip , HttpRequest *r ,
  91. char *q , int32_t qlen , int32_t s , int32_t n ) ;
  92. // . used by the HttpPageX.h classes after making their dynamic content
  93. // . returns false if blocked, true otherwise
  94. // . sets errno on error
  95. // . a cacheTime of -2 means browser should not cache when user
  96. // is clicking forward or hitting back button OR anytime -- no cache!
  97. // . a cacheTime of -1 means browser should not cache when user
  98. // is clicking forward, but caching when clicking back button is ok
  99. // . a cacheTime of 0 tells browser to use local caching rules
  100. bool sendDynamicPage ( TcpSocket *s , const char *page , int32_t pageLen ,
  101. int32_t cacheTime = -1 , bool POSTReply = false ,
  102. const char *contentType = NULL,
  103. int32_t httpStatus = -1,
  104. const char *cookie = NULL,
  105. const char *charset = NULL ,
  106. HttpRequest *hr = NULL );
  107. // for PageSockets
  108. TcpServer *getTcp() {
  109. return &m_tcp;
  110. }
  111. TcpServer *getSSLTcp() {
  112. return &m_ssltcp;
  113. }
  114. // we contain our own tcp server
  115. TcpServer m_tcp;
  116. TcpServer m_ssltcp;
  117. // cancel the transaction that had this state
  118. void cancel ( void *state ) {
  119. m_tcp.cancel ( state );
  120. }
  121. int32_t m_maxOpenSockets;
  122. //for content-encoding: gzip, we unzip the reply and edit the
  123. //header to reflect the new size and encoding
  124. TcpSocket *unzipReply(TcpSocket* s);
  125. float getCompressionRatio() {
  126. if ( m_bytesDownloaded )
  127. return (float)m_uncompressedBytes/m_bytesDownloaded;
  128. else
  129. return 0.0;
  130. }
  131. bool processSquidProxyRequest ( TcpSocket *sock, HttpRequest *hr);
  132. // private:
  133. // go ahead and start sending the file ("path") over the socket
  134. bool sendReply ( TcpSocket *s , HttpRequest *r , bool isAdmin);
  135. bool sendReply2 ( const char *mime,
  136. int32_t mimeLen ,
  137. const char *content,
  138. int32_t contentLen ,
  139. TcpSocket *s ,
  140. bool alreadyCompressed = false ,
  141. HttpRequest *hr = NULL) ;
  142. void *states[MAX_DOWNLOADS];
  143. tcp_callback_t callbacks[MAX_DOWNLOADS];
  144. int64_t m_bytesDownloaded;
  145. int64_t m_uncompressedBytes;
  146. };
  147. extern class HttpServer g_httpServer;
  148. #endif // GB_HTTPSERVER_H