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