forked from dreiss/old-thrift
/
THttpServer.cpp
95 lines (77 loc) · 2.24 KB
/
THttpServer.cpp
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
#include <cstdlib>
#include <sstream>
#include <iostream>
#include "THttpServer.h"
#include "TSocket.h"
namespace facebook { namespace thrift { namespace transport {
using namespace std;
/**
* Http server transport implementation.
*
* @author Jeremie BORDIER <jeremie.bordier@gmail.com>
*/
THttpServer::THttpServer(boost::shared_ptr<TTransport> transport) :
THttpTransport(transport) {
}
THttpServer::~THttpServer() {}
void THttpServer::parseHeader(char* header) {
char* colon = strchr(header, ':');
if (colon == NULL) {
return;
}
uint32_t sz = colon - header;
char* value = colon+1;
if (strncmp(header, "Transfer-Encoding", sz) == 0) {
if (strstr(value, "chunked") != NULL) {
chunked_ = true;
}
} else if (strncmp(header, "Content-Length", sz) == 0) {
chunked_ = false;
contentLength_ = atoi(value);
}
}
bool THttpServer::parseStatusLine(char* status) {
char* method = status;
char* path = strchr(method, ' ');
if (path == NULL) {
throw TTransportException(string("Bad Status: ") + status);
}
*path = '\0';
while (*(++path) == ' ');
char* http = strchr(path, ' ');
if (http == NULL) {
throw TTransportException(string("Bad Status: ") + status);
}
*http = '\0';
if (strcmp(method, "POST") == 0) {
// POST method ok, looking for content.
return true;
}
throw TTransportException(string("Bad Status (unsupported method): ") + status);
}
void THttpServer::flush() {
// Fetch the contents of the write buffer
uint8_t* buf;
uint32_t len;
writeBuffer_.getBuffer(&buf, &len);
// Construct the HTTP header
std::ostringstream h;
h <<
"HTTP/1.1 200 Ok" << CRLF <<
"Date: Fri, 02 Jan 2009 19:09:44 GMT" << CRLF <<
"Server: Thrift/0.1" << CRLF <<
"Content-Type: application/x-thrift" << CRLF <<
"Content-Length: " << len << CRLF <<
"Connection: Keep-Alive" << CRLF <<
CRLF;
string header = h.str();
// Write the header, then the data, then flush
transport_->write((const uint8_t*)header.c_str(), header.size());
transport_->write(buf, len);
transport_->flush();
// printf(">>>\n%s%s\n", header.c_str(), buf);
// Reset the buffer and header variables
writeBuffer_.resetBuffer();
readHeaders_ = true;
}
}}} // facebook::thrift::transport