qhttpconnection.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2011-2014 Nikhil Marathe <nsm.nikhil@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #ifndef Q_HTTP_CONNECTION
  23. #define Q_HTTP_CONNECTION
  24. #include "qhttpserverapi.h"
  25. #include "qhttpserverfwd.h"
  26. #include <QObject>
  27. /// @cond nodoc
  28. class QHTTPSERVER_API QHttpConnection : public QObject
  29. {
  30. Q_OBJECT
  31. public:
  32. QHttpConnection(QTcpSocket *socket, QObject *parent = 0);
  33. virtual ~QHttpConnection();
  34. void write(const QByteArray &data);
  35. void flush();
  36. void waitForBytesWritten();
  37. Q_SIGNALS:
  38. void newRequest(QHttpRequest *, QHttpResponse *);
  39. void allBytesWritten();
  40. private Q_SLOTS:
  41. void parseRequest();
  42. void responseDone();
  43. void socketDisconnected();
  44. void updateWriteCount(qint64);
  45. private:
  46. static int MessageBegin(http_parser *parser);
  47. static int Url(http_parser *parser, const char *at, size_t length);
  48. static int HeaderField(http_parser *parser, const char *at, size_t length);
  49. static int HeaderValue(http_parser *parser, const char *at, size_t length);
  50. static int HeadersComplete(http_parser *parser);
  51. static int Body(http_parser *parser, const char *at, size_t length);
  52. static int MessageComplete(http_parser *parser);
  53. private:
  54. QTcpSocket *m_socket;
  55. http_parser *m_parser;
  56. http_parser_settings *m_parserSettings;
  57. // Since there can only be one request at any time even with pipelining.
  58. QHttpRequest *m_request;
  59. QByteArray m_currentUrl;
  60. // The ones we are reading in from the parser
  61. HeaderHash m_currentHeaders;
  62. QString m_currentHeaderField;
  63. QString m_currentHeaderValue;
  64. // Keep track of transmit buffer status
  65. qint64 m_transmitLen;
  66. qint64 m_transmitPos;
  67. };
  68. /// @endcond
  69. #endif