#include <stdint.h>
#include <assert.h>
#include <arpa/inet.h>
-#include <curl/curl.h>
#include <sys/socket.h>
#include <pthread.h>
#include <sys/types.h>
for (map<string, Stream *>::iterator stream_it = streams.begin();
stream_it != streams.end();
++stream_it) {
- Stream *stream = stream_it->second;
- for (size_t i = 0; i < stream->to_process.size(); ++i) {
- process_client(stream->to_process[i]);
+ vector<Client *> to_process;
+ swap(stream_it->second->to_process, to_process);
+ for (size_t i = 0; i < to_process.size(); ++i) {
+ process_client(to_process[i]);
}
- stream->to_process.clear();
}
}
}
return;
}
- // Guard against overlong requests gobbling up all of our space.
- if (client->request.size() + ret > MAX_CLIENT_REQUEST) {
+ RequestParseStatus status = wait_for_double_newline(&client->request, buf, ret);
+
+ switch (status) {
+ case RP_OUT_OF_SPACE:
fprintf(stderr, "WARNING: fd %d sent overlong request!\n", client->sock);
close_client(client);
return;
- }
-
- // See if we have \r\n\r\n anywhere in the request. We start three bytes
- // before what we just appended, in case we just got the final character.
- size_t existing_req_bytes = client->request.size();
- client->request.append(string(buf, buf + ret));
-
- size_t start_at = (existing_req_bytes >= 3 ? existing_req_bytes - 3 : 0);
- const char *ptr = reinterpret_cast<char *>(
- memmem(client->request.data() + start_at, client->request.size() - start_at,
- "\r\n\r\n", 4));
- if (ptr == NULL) {
+ case RP_NOT_FINISHED_YET:
// OK, we don't have the entire header yet. Fine; we'll get it later.
// See if there's more data for us.
goto read_request_again;
- }
-
- if (ptr != client->request.data() + client->request.size() - 4) {
+ case RP_EXTRA_DATA:
fprintf(stderr, "WARNING: fd %d had junk data after request!\n", client->sock);
close_client(client);
return;
+ case RP_FINISHED:
+ break;
}
+ assert(status == RP_FINISHED);
+
int error_code = parse_request(client);
if (error_code == 200) {
construct_header(client);
exit(1);
}
}
+
+template<class T>
+void delete_from(vector<T> *v, T elem)
+{
+ typename vector<T>::iterator new_end = remove(v->begin(), v->end(), elem);
+ v->erase(new_end, v->end());
+}
void Server::close_client(Client *client)
{
// This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
if (client->stream != NULL) {
- vector<Client *>::iterator new_end =
- remove(client->stream->sleeping_clients.begin(),
- client->stream->sleeping_clients.end(),
- client);
- client->stream->sleeping_clients.erase(
- new_end, client->stream->sleeping_clients.end());
+ delete_from(&client->stream->sleeping_clients, client);
+ delete_from(&client->stream->to_process, client);
}
// Bye-bye!