HTTPConnection.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file HTTPConnection.h
  11. /// \brief Contains HTTPConnection, used to communicate with web servers
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_HTTPConnection==1 && _RAKNET_SUPPORT_TCPInterface==1
  15. #ifndef __HTTP_CONNECTION
  16. #define __HTTP_CONNECTION
  17. #include "Export.h"
  18. #include "RakString.h"
  19. #include "RakMemoryOverride.h"
  20. #include "RakNetTypes.h"
  21. #include "DS_Queue.h"
  22. namespace RakNet
  23. {
  24. /// Forward declarations
  25. class TCPInterface;
  26. struct SystemAddress;
  27. /// \brief Use HTTPConnection to communicate with a web server.
  28. /// \details Start an instance of TCPInterface via the Start() command.
  29. /// Instantiate a new instance of HTTPConnection, and associate TCPInterface with the class in the constructor.
  30. /// Use Post() to send commands to the web server, and ProcessDataPacket() to update the connection with packets returned from TCPInterface that have the system address of the web server
  31. /// This class will handle connecting and reconnecting as necessary.
  32. ///
  33. /// Note that only one Post() can be handled at a time.
  34. /// \deprecated, use HTTPConnection2
  35. class RAK_DLL_EXPORT HTTPConnection
  36. {
  37. public:
  38. // GetInstance() and DestroyInstance(instance*)
  39. STATIC_FACTORY_DECLARATIONS(HTTPConnection)
  40. /// Returns a HTTP object associated with this tcp connection
  41. HTTPConnection();
  42. virtual ~HTTPConnection();
  43. /// \pre tcp should already be started
  44. void Init(TCPInterface *_tcp, const char *host, unsigned short port=80);
  45. /// Submit data to the HTTP server
  46. /// HTTP only allows one request at a time per connection
  47. ///
  48. /// \pre IsBusy()==false
  49. /// \param path the path on the remote server you want to POST to. For example "index.html"
  50. /// \param data A NULL terminated string to submit to the server
  51. /// \param contentType "Content-Type:" passed to post.
  52. void Post(const char *path, const char *data, const char *_contentType="application/x-www-form-urlencoded");
  53. /// Get a file from a webserver
  54. /// \param path the path on the remote server you want to GET from. For example "index.html"
  55. void Get(const char *path);
  56. /// Is there a Read result ready?
  57. bool HasRead(void) const;
  58. /// Get one result from the server
  59. /// \pre HasResult must return true
  60. RakNet::RakString Read(void);
  61. /// Call periodically to do time-based updates
  62. void Update(void);
  63. /// Returns the address of the server we are connected to
  64. SystemAddress GetServerAddress(void) const;
  65. /// Process an HTTP data packet returned from TCPInterface
  66. /// Returns true when we have gotten all the data from the HTTP server.
  67. /// If this returns true then it's safe to Post() another request
  68. /// Deallocate the packet as usual via TCPInterface
  69. /// \param packet NULL or a packet associated with our host and port
  70. void ProcessTCPPacket(Packet *packet);
  71. /// Results of HTTP requests. Standard response codes are < 999
  72. /// ( define HTTP codes and our internal codes as needed )
  73. enum ResponseCodes { NoBody=1001, OK=200, Deleted=1002 };
  74. HTTPConnection& operator=(const HTTPConnection& rhs){(void) rhs; return *this;}
  75. /// Encapsulates a raw HTTP response and response code
  76. struct BadResponse
  77. {
  78. public:
  79. BadResponse() {code=0;}
  80. BadResponse(const unsigned char *_data, int _code)
  81. : data((const char *)_data), code(_code) {}
  82. BadResponse(const char *_data, int _code)
  83. : data(_data), code(_code) {}
  84. operator int () const { return code; }
  85. RakNet::RakString data;
  86. int code; // ResponseCodes
  87. };
  88. /// Queued events of failed exchanges with the HTTP server
  89. bool HasBadResponse(int *code, RakNet::RakString *data);
  90. /// Returns false if the connection is not doing anything else
  91. bool IsBusy(void) const;
  92. /// \internal
  93. int GetState(void) const;
  94. struct OutgoingCommand
  95. {
  96. RakNet::RakString remotePath;
  97. RakNet::RakString data;
  98. RakNet::RakString contentType;
  99. bool isPost;
  100. };
  101. DataStructures::Queue<OutgoingCommand> outgoingCommand;
  102. OutgoingCommand currentProcessingCommand;
  103. private:
  104. SystemAddress server;
  105. TCPInterface *tcp;
  106. RakNet::RakString host;
  107. unsigned short port;
  108. DataStructures::Queue<BadResponse> badResponses;
  109. enum ConnectionState
  110. {
  111. CS_NONE,
  112. CS_DISCONNECTING,
  113. CS_CONNECTING,
  114. CS_CONNECTED,
  115. CS_PROCESSING,
  116. } connectionState;
  117. RakNet::RakString incomingData;
  118. DataStructures::Queue<RakNet::RakString> results;
  119. void CloseConnection();
  120. /*
  121. enum { RAK_HTTP_INITIAL,
  122. RAK_HTTP_STARTING,
  123. RAK_HTTP_CONNECTING,
  124. RAK_HTTP_ESTABLISHED,
  125. RAK_HTTP_REQUEST_SENT,
  126. RAK_HTTP_IDLE } state;
  127. RakNet::RakString outgoing, incoming, path, contentType;
  128. void Process(Packet *packet); // the workhorse
  129. // this helps check the various status lists in TCPInterface
  130. typedef SystemAddress (TCPInterface::*StatusCheckFunction)(void);
  131. bool InList(StatusCheckFunction func);
  132. */
  133. };
  134. } // namespace RakNet
  135. #endif
  136. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号