36 void receive_data(
const Datagram &data);
37 void receive_line(
string line);
50 _received += data.get_message();
52 size_t newline = _received.find(
'\n', next);
53 while (newline != string::npos) {
56 if (newline > 0 && _received[newline - 1] ==
'\r') {
59 receive_line(_received.substr(last, newline - last));
60 if (next < _received.size() && _received[next] ==
'\r') {
63 newline = _received.find(
'\n', next);
65 _received = _received.substr(next);
69 receive_line(
string line) {
70 std::cerr <<
"received: " << line <<
"\n";
72 size_t size = line.size();
73 while (size > 0 && isspace(line[size - 1])) {
76 if (size != line.size()) {
77 line = line.substr(0, size);
94 main(
int argc,
char *argv[]) {
96 nout <<
"fake_http_server port\n";
100 int port = atoi(argv[1]);
102 PT(
Connection) rendezvous = cm.open_TCP_server_rendezvous(port, 5);
104 if (rendezvous.is_null()) {
105 nout <<
"Cannot grab port " << port <<
".\n";
109 nout <<
"Listening for connections on port " << port <<
"\n";
112 listener.add_connection(rendezvous);
117 reader.set_raw_mode(1);
118 writer.set_raw_mode(1);
120 bool shutdown =
false;
123 while (listener.new_connection_available()) {
127 if (listener.get_new_connection(rv, address, new_connection)) {
128 nout <<
"Got connection from " << address <<
"\n";
129 reader.add_connection(new_connection);
130 clients.insert(Clients::value_type(new_connection, ClientState(new_connection)));
138 nout <<
"Lost connection from "
139 << connection->get_address() <<
"\n";
140 clients.erase(connection);
141 cm.close_connection(connection);
146 while (reader.data_available()) {
148 if (reader.get_data(datagram)) {
149 PT(
Connection) client = datagram.get_connection();
150 Clients::iterator ci = clients.find(client);
151 if (ci == clients.end()) {
152 nout <<
"Received data from unexpected client " << (
void *)client
155 ClientState &state = (*ci).second;
156 state.receive_data(datagram);