BBS2chProxyRawSocket.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <sstream>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #ifdef _WIN32
  6. #include <winsock2.h>
  7. #include <windows.h>
  8. #define CLOSESOCKET(x) closesocket(x)
  9. #define SHUT_RDWR SD_BOTH
  10. #define gmtime_r(a, b) gmtime_s(b, a)
  11. #else
  12. #include <sys/socket.h>
  13. #define CLOSESOCKET(x) ::close(x)
  14. #endif
  15. #include "BBS2chProxyRawSocket.h"
  16. #define HTTP_TIMESTAMP_FMT_PRINT "%a, %d %b %Y %H:%M:%S GMT"
  17. BBS2chProxyRawSocket::BBS2chProxyRawSocket(int sock) :
  18. socket(sock)
  19. {
  20. }
  21. BBS2chProxyRawSocket::~BBS2chProxyRawSocket()
  22. {
  23. }
  24. int BBS2chProxyRawSocket::read(char *buffer, int length)
  25. {
  26. return recv(socket, buffer, length, 0);
  27. }
  28. int BBS2chProxyRawSocket::readLine(char *buffer, int maxLength)
  29. {
  30. char *ptr = buffer;
  31. while (ptr < buffer + maxLength - 1) {
  32. int read = recv(socket, ptr, 1, 0);
  33. if (read != 1) {
  34. return 0;
  35. }
  36. if (*ptr++ == '\n') {
  37. break;
  38. }
  39. }
  40. *ptr = 0;
  41. return 1;
  42. }
  43. int BBS2chProxyRawSocket::write(const char *buffer, int length)
  44. {
  45. return send(socket, buffer, length, 0);
  46. }
  47. int BBS2chProxyRawSocket::writeString(const std::string &str)
  48. {
  49. return send(socket, str.data(), str.length(), 0);
  50. }
  51. void BBS2chProxyRawSocket::close(void)
  52. {
  53. if (socket >= 0) {
  54. shutdown(socket, SHUT_RDWR);
  55. #ifdef _WIN32
  56. Sleep(10);
  57. #else
  58. usleep(10000);
  59. #endif
  60. CLOSESOCKET(socket);
  61. socket = -1;
  62. }
  63. }
  64. void IBBS2chProxySocket::sendBasicHeaders(int respCode, const char *respMsg)
  65. {
  66. char date[256];
  67. time_t now = time(0);
  68. struct tm tm = {0};
  69. gmtime_r(&now, &tm);
  70. strftime(date, 256, HTTP_TIMESTAMP_FMT_PRINT, &tm);
  71. std::ostringstream ss;
  72. ss << "HTTP/1.1 " << respCode << " " << respMsg << "\r\n";
  73. if (0 >= this->writeString(ss.str())) return;
  74. if (0 >= this->writeString("Connection: Close\r\n")) return;
  75. if (0 >= this->writeString("Server: proxy2ch\r\n")) return;
  76. if (0 >= this->writeString(std::string("Date: ") + date + "\r\n")) return;
  77. }
  78. void IBBS2chProxySocket::sendResponse(int respCode, const char *respMsg)
  79. {
  80. sendBasicHeaders(respCode, respMsg);
  81. if (0 >= this->writeString("Content-Type: text/plain; charset=UTF-8\r\n")) return;
  82. if (0 >= this->writeString("\r\n")) return;
  83. if (respCode >= 400) {
  84. if (0 >= this->writeString("   ∧_∧   / ̄ ̄ ̄ ̄ ̄\n")) return;
  85. if (0 >= this->writeString(std::string("  ( ´∀`)< ") + respMsg + "\n")) return;
  86. if (0 >= this->writeString("  (    ) \_____\n")) return;
  87. if (0 >= this->writeString("   │ │ │\n")) return;
  88. if (0 >= this->writeString("  (__)_)\n")) return;
  89. }
  90. }