BBS2chProxyConnection.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <string>
  3. #if (defined(__clang__) && defined(_LIBCPP_VERSION)) || (__cplusplus >= 201103L)
  4. #include <memory>
  5. #else
  6. #include <tr1/memory>
  7. #endif
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <pthread.h>
  11. #include <sys/types.h>
  12. #include <regex.h>
  13. #include <curl/curl.h>
  14. #include "DataStorage.h"
  15. #include "BBS2chProxyThreadInfo.h"
  16. #include "BBS2chProxyAuth.h"
  17. #include "IBBS2chProxySocket.h"
  18. #include "BBS2chProxyHttpHeaders.h"
  19. #include "BBS2chProxyURL.h"
  20. #include "BBS2chProxyKeyManager.h"
  21. struct BBS2chThreadIdentifier {
  22. std::string hostPrefix;
  23. std::string host;
  24. std::string board;
  25. std::string key;
  26. };
  27. class BBS2chProxyConnection {
  28. private:
  29. int sock_c;
  30. int serverPort;
  31. BBS2chProxyThreadCache *threadCache;
  32. std::string threadKey;
  33. pthread_mutex_t *mutex;
  34. static BBS2chProxyAuth auth;
  35. BBS2chProxyURL requestURL;
  36. public:
  37. int content_length;
  38. int status;
  39. bool isResponseChunked;
  40. bool force5ch;
  41. bool bbscgi;
  42. std::string responseHeaders;
  43. IBBS2chProxySocket *socketToClient;
  44. bool isClientChunked;
  45. bool isClientHttp1_0;
  46. CURL *curl;
  47. bool isHttps;
  48. std::string monaKeyForRequest;
  49. std::string userAgentForRequest;
  50. int directDatDownloading;
  51. static BBS2chProxyKeyManager keyManager;
  52. public:
  53. BBS2chProxyConnection(int socket, int port, BBS2chProxyThreadCache *cache, pthread_mutex_t *m) :
  54. sock_c(socket), serverPort(port), threadCache(cache), mutex(m),
  55. content_length(0), status(0), isResponseChunked(false), force5ch(false), bbscgi(false),
  56. socketToClient(NULL), isClientChunked(false), isClientHttp1_0(false), curl(NULL), isHttps(false), directDatDownloading(0)
  57. {
  58. };
  59. ~BBS2chProxyConnection()
  60. {
  61. if (socketToClient) delete socketToClient;
  62. };
  63. void run(void * (*func)(void *));
  64. void connect(void);
  65. void setMonaKey(const std::string &key, int reason);
  66. static void compileRegex(void);
  67. private:
  68. int datProxy(const char *url, const char *method, BBS2chProxyHttpHeaders &requestHeaders);
  69. int datProxyAPI(const char *url, const char *method, BBS2chProxyHttpHeaders &requestHeaders);
  70. int bbsmenuProxy(const char *url, const char *method, BBS2chProxyHttpHeaders &requestHeaders);
  71. int bbsCgiProxy(const char *url, BBS2chProxyHttpHeaders &requestHeaders, const char *requestBody);
  72. DataStorage *html2dat_old(DataStorage *html, int startResNum, time_t *lastModified, bool useCache);
  73. DataStorage *html2dat(DataStorage *html, int startResNum, time_t *lastModified, bool useCache);
  74. int tunnel(const char *addr, int port);
  75. std::string getMonaKey();
  76. BBS2chProxyURL getRawDatURLAndStatus(const BBS2chThreadIdentifier &threadIdentifier, BBS2chProxyHttpHeaders &requestHeaders, bool findKakologOnly, long *status, bool *foundAsKakolog);
  77. };
  78. #if (defined(__clang__) && defined(_LIBCPP_VERSION)) || (__cplusplus >= 201103L)
  79. typedef std::shared_ptr<BBS2chProxyConnection> PBBS2chProxyConnection;
  80. #else
  81. typedef std::tr1::shared_ptr<BBS2chProxyConnection> PBBS2chProxyConnection;
  82. #endif