+ vector<string> request_tokens = split_tokens(lines[0]);
+ if (request_tokens.size() < 2) {
+ return 400; // Bad request (empty).
+ }
+ if (request_tokens[0] != "GET") {
+ return 400; // Should maybe be 405 instead?
+ }
+
+ map<string, int>::const_iterator url_map_it = url_map.find(request_tokens[1]);
+ if (url_map_it == url_map.end()) {
+ return 404; // Not found.
+ }
+
+ client->url = request_tokens[1];
+ client->stream = streams[url_map_it->second];
+ if (client->stream->mark_pool != NULL) {
+ client->fwmark = client->stream->mark_pool->get_mark();
+ } else {
+ client->fwmark = 0; // No mark.
+ }
+ if (setsockopt(client->sock, SOL_SOCKET, SO_MARK, &client->fwmark, sizeof(client->fwmark)) == -1) {
+ if (client->fwmark != 0) {
+ log_perror("setsockopt(SO_MARK)");
+ }
+ }
+ client->request.clear();
+
+ return 200; // OK!
+}
+
+void Server::construct_header(Client *client)
+{
+ Stream *stream = client->stream;
+ if (stream->encoding == Stream::STREAM_ENCODING_RAW) {
+ client->header_or_error = stream->http_header +
+ "\r\n" +
+ stream->stream_header;
+ } else if (stream->encoding == Stream::STREAM_ENCODING_METACUBE) {
+ client->header_or_error = stream->http_header +
+ "Content-encoding: metacube\r\n" +
+ "\r\n";
+ if (!stream->stream_header.empty()) {
+ metacube_block_header hdr;
+ memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
+ hdr.size = htonl(stream->stream_header.size());
+ hdr.flags = htonl(METACUBE_FLAGS_HEADER);
+ client->header_or_error.append(
+ string(reinterpret_cast<char *>(&hdr), sizeof(hdr)));
+ }
+ client->header_or_error.append(stream->stream_header);
+ } else {
+ assert(false);
+ }