HttpHeaders.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include "HttpHeaders.hpp"
  20. #include "../util/Util.hpp"
  21. #include "../Logger.hpp"
  22. std::map<std::string, std::string, ci_less>& HttpHeaders::getHeaders() {
  23. return headers;
  24. }
  25. std::string& HttpHeaders::getHeader(std::string &header) {
  26. return headers[header];
  27. }
  28. bool HttpHeaders::isPost() {
  29. return method == std::string("POST");
  30. }
  31. std::string& HttpHeaders::getMethod() {
  32. return method;
  33. }
  34. std::string& HttpHeaders::getRequest() {
  35. return request;
  36. }
  37. std::string& HttpHeaders::getPostData() {
  38. return postData;
  39. }
  40. int HttpHeaders::readLine(char *buffer, int *offset, int length) {
  41. int i;
  42. for (i=*offset;i<length;i++) {
  43. if (buffer[i] == '\n') foundLf = 1;
  44. else if (buffer[i] == '\r') {foundCr = 1; foundLf = 0;}
  45. else {foundCr = 0; foundLf = 0;}
  46. if (foundCr && foundLf) {
  47. foundCr = 0;
  48. foundLf = 0;
  49. *offset = i+1;
  50. return 1;
  51. }
  52. }
  53. *offset = i;
  54. return 0;
  55. }
  56. void HttpHeaders::parseAction() {
  57. Util::trimString(action);
  58. size_t methodEnd = action.find(" ");
  59. size_t requestEnd = action.find_last_of(" ");
  60. if (methodEnd == std::string::npos || requestEnd == std::string::npos)
  61. throw HttpHeaderException();
  62. method = action.substr(0, methodEnd);
  63. request = action.substr(methodEnd+1, requestEnd - methodEnd);
  64. Util::trimString(method);
  65. Util::trimString(request);
  66. // std::cerr << "Read Action: " << action << std::endl;
  67. // std::cerr << "Method: " << method << std::endl;
  68. // std::cerr << "Request: " << request << std::endl;
  69. }
  70. int HttpHeaders::readAction(char *buffer, int length) {
  71. int offset = 0;
  72. int complete = readLine(buffer, &offset, length);
  73. action.append(buffer, offset);
  74. if (complete) {
  75. parseAction();
  76. this->state = READING_KEY;
  77. }
  78. return offset;
  79. }
  80. int HttpHeaders::readValue(char *buffer, int offset, int length) {
  81. int eolOffset = offset;
  82. int complete = readLine(buffer, &eolOffset, length);
  83. this->value.append(buffer+offset, (eolOffset - offset));
  84. if (complete) {
  85. this->state = READING_KEY;
  86. this->headers[key] = value;
  87. // std::cerr << "Found value: " << this->value << std::endl;
  88. this->key.clear();
  89. this->value.clear();
  90. }
  91. return eolOffset;
  92. }
  93. int HttpHeaders::readKey(char *buffer, int offset, int length) {
  94. int i;
  95. if (offset < length && buffer[offset] == '\r') {
  96. if (isPost()) this->state = READING_POST;
  97. else this->state = READING_DONE;
  98. return offset + 2;
  99. }
  100. for (i=offset;i<length;i++) {
  101. if (buffer[i] == ':') {
  102. this->state = READING_VALUE;
  103. break;
  104. }
  105. }
  106. this->key.append(buffer+offset, (i-offset));
  107. // if (this->state == READING_VALUE)
  108. // std::cerr << "Found Key: " << this->key << " at " << i << std::endl;
  109. return i+1;
  110. }
  111. int HttpHeaders::getContentLength() {
  112. std::string contentKey = "Content-Length";
  113. std::string contentLengthStr = getHeader(contentKey);
  114. int contentLength;
  115. if (!Util::fromString<int>(contentLength, contentLengthStr, std::dec)) {
  116. Logger::logError("Could not read POST data without Content-Length...");
  117. return 0;
  118. }
  119. return contentLength;
  120. }
  121. void HttpHeaders::readPostData(char *buffer, int offset, int length) {
  122. int contentLength = getContentLength();
  123. int contentLeft = contentLength - postData.length();
  124. int dataLeft = length - offset;
  125. int copyLength = (dataLeft < contentLeft) ? dataLeft : contentLeft;
  126. postData.append(buffer, offset, copyLength);
  127. if (postData.length() >= contentLength) {
  128. state = READING_DONE;
  129. // std::cerr << "*** Got POST data: " << postData << std::endl;
  130. }
  131. }
  132. bool HttpHeaders::process(char *buffer, int length) {
  133. int offset = 0;
  134. while (offset < length) {
  135. switch(state) {
  136. case READING_ACTION: offset = readAction(buffer, length); break;
  137. case READING_KEY: offset = readKey(buffer, offset, length); break;
  138. case READING_VALUE: offset = readValue(buffer, offset, length); break;
  139. case READING_POST: readPostData(buffer, offset, length); break;
  140. case READING_DONE: return true;
  141. }
  142. }
  143. if (state == READING_DONE) return true;
  144. else return false;
  145. }