HttpRequest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "HttpRequest.h"
  24. #include "Log.h"
  25. #include "Profiler.h"
  26. #include "StringUtils.h"
  27. #include <civetweb.h>
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. static const unsigned ERROR_BUFFER_LEN = 256;
  32. HttpRequest::HttpRequest(const String& url, const String& verb, const Vector<String>& headers, const String& postData) :
  33. url_(url.Trimmed()),
  34. verb_(!verb.Empty() ? verb : "GET"),
  35. connection_(0)
  36. {
  37. // Size of response is unknown, so just set maximum value. The position will also be changed
  38. // to maximum value once the request is done, signaling end for Deserializer::IsEof().
  39. size_ = M_MAX_UNSIGNED;
  40. String protocol = "http";
  41. String host;
  42. String path = "/";
  43. int port = 80;
  44. unsigned protocolEnd = url_.Find("://");
  45. if (protocolEnd != String::NPOS)
  46. {
  47. protocol = url_.Substring(0, protocolEnd);
  48. host = url_.Substring(protocolEnd + 3);
  49. }
  50. else
  51. host = url_;
  52. unsigned pathStart = host.Find('/');
  53. if (pathStart != String::NPOS)
  54. {
  55. path = host.Substring(pathStart);
  56. host = host.Substring(0, pathStart);
  57. }
  58. unsigned portStart = host.Find(':');
  59. if (portStart != String::NPOS)
  60. {
  61. port = ToInt(host.Substring(portStart + 1));
  62. host = host.Substring(0, portStart);
  63. }
  64. LOGDEBUG("HTTP " + verb_ + " request to host " + host + " port " + String(port) + " path " + String(path));
  65. char errorBuffer[ERROR_BUFFER_LEN];
  66. memset(errorBuffer, 0, sizeof(errorBuffer));
  67. String headersStr;
  68. for (unsigned i = 0; i < headers.Size(); ++i)
  69. {
  70. // Trim and only add non-empty header strings
  71. String header = headers[i].Trimmed();
  72. if (header.Length())
  73. headersStr += header + "\r\n";
  74. }
  75. /// \todo SSL mode will not actually work unless Civetweb's SSL mode is initialized with an external SSL DLL
  76. if (postData.Empty())
  77. {
  78. connection_ = mg_download(host.CString(), port, protocol.Compare("https", false) ? 0 : 1, errorBuffer, sizeof(errorBuffer),
  79. "%s %s HTTP/1.0\r\n"
  80. "Host: %s\r\n"
  81. "%s"
  82. "\r\n", verb_.CString(), path.CString(), host.CString(), headersStr.CString());
  83. }
  84. else
  85. {
  86. connection_ = mg_download(host.CString(), port, protocol.Compare("https", false) ? 0 : 1, errorBuffer, sizeof(errorBuffer),
  87. "%s %s HTTP/1.0\r\n"
  88. "Host: %s\r\n"
  89. "%s"
  90. "Content-Length: %d\r\n"
  91. "\r\n"
  92. "%s", verb_.CString(), path.CString(), host.CString(), headersStr.CString(), postData.Length(), postData.CString());
  93. }
  94. if (!connection_)
  95. error_ = String(&errorBuffer[0]);
  96. }
  97. HttpRequest::~HttpRequest()
  98. {
  99. Release();
  100. }
  101. unsigned HttpRequest::Read(void* dest, unsigned size)
  102. {
  103. if (!connection_)
  104. return 0;
  105. int bytesRead = mg_read((mg_connection*)connection_, dest, size);
  106. // Automatically close the connection if no more data
  107. if (bytesRead <= 0)
  108. Release();
  109. return bytesRead > 0 ? bytesRead : 0;
  110. }
  111. void HttpRequest::Release()
  112. {
  113. if (connection_)
  114. {
  115. mg_close_connection((mg_connection*)connection_);
  116. connection_ = 0;
  117. position_ = M_MAX_UNSIGNED;
  118. }
  119. }
  120. }