X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=server.cpp;h=9b83ddccd4fd0ff53f39bd08a702495312282d5b;hp=615e8fb31d48b137df3cdcb3acf8caa4cbf00c68;hb=d0aad5446b9896e3ba15c8a50c345e185dc1631f;hpb=38a2bb28fd8dcb5bb1e0cb56028936a35f20f503 diff --git a/server.cpp b/server.cpp index 615e8fb..9b83ddc 100644 --- a/server.cpp +++ b/server.cpp @@ -288,17 +288,17 @@ void Server::add_client_from_serialized(const ClientProto &client) int Server::lookup_stream_by_url(const string &url) const { - map::const_iterator url_it = url_map.find(url); - if (url_it == url_map.end()) { + map::const_iterator stream_url_it = stream_url_map.find(url); + if (stream_url_it == stream_url_map.end()) { return -1; } - return url_it->second; + return stream_url_it->second; } int Server::add_stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Stream::Encoding encoding) { MutexLock lock(&mutex); - url_map.insert(make_pair(url, streams.size())); + stream_url_map.insert(make_pair(url, streams.size())); streams.push_back(new Stream(url, backlog_size, prebuffering_bytes, encoding)); return streams.size() - 1; } @@ -306,7 +306,7 @@ int Server::add_stream(const string &url, size_t backlog_size, size_t prebufferi int Server::add_stream_from_serialized(const StreamProto &stream, int data_fd) { MutexLock lock(&mutex); - url_map.insert(make_pair(stream.url(), streams.size())); + stream_url_map.insert(make_pair(stream.url(), streams.size())); streams.push_back(new Stream(stream, data_fd)); return streams.size() - 1; } @@ -359,6 +359,13 @@ void Server::set_pacing_rate(int stream_index, uint32_t pacing_rate) streams[stream_index]->pacing_rate = pacing_rate; } +void Server::add_gen204(const std::string &url, const std::string &allow_origin) +{ + MutexLock lock(&mutex); + assert(clients.empty()); + ping_url_map[url] = allow_origin; +} + void Server::add_data_deferred(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start) { assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); @@ -418,6 +425,8 @@ read_request_again: int error_code = parse_request(client); if (error_code == 200) { construct_header(client); + } else if (error_code == 204) { + construct_204(client); } else { construct_error(client, error_code); } @@ -636,6 +645,7 @@ int Server::parse_request(Client *client) } string url = request_tokens[1]; + client->url = url; if (url.find("?backlog") == url.size() - 8) { client->stream_pos = -2; url = url.substr(0, url.size() - 8); @@ -643,18 +653,21 @@ int Server::parse_request(Client *client) client->stream_pos = -1; } - map::const_iterator url_map_it = url_map.find(url); - if (url_map_it == url_map.end()) { - return 404; // Not found. + map::const_iterator stream_url_map_it = stream_url_map.find(url); + if (stream_url_map_it == stream_url_map.end()) { + map::const_iterator ping_url_map_it = ping_url_map.find(url); + if (ping_url_map_it == ping_url_map.end()) { + return 404; // Not found. + } else { + return 204; // No error. + } } - Stream *stream = streams[url_map_it->second]; + Stream *stream = streams[stream_url_map_it->second]; if (stream->http_header.empty()) { return 503; // Service unavailable. } - client->url = request_tokens[1]; - client->stream = stream; if (setsockopt(client->sock, SOL_SOCKET, SO_MAX_PACING_RATE, &client->stream->pacing_rate, sizeof(client->stream->pacing_rate)) == -1) { if (client->stream->pacing_rate != ~0U) { @@ -724,6 +737,38 @@ void Server::construct_error(Client *client, int error_code) } } +void Server::construct_204(Client *client) +{ + map::const_iterator ping_url_map_it = ping_url_map.find(client->url); + assert(ping_url_map_it != ping_url_map.end()); + + if (ping_url_map_it->second.empty()) { + client->header_or_short_response = + "HTTP/1.0 204 No Content\r\n" + "\r\n"; + } else { + char response[256]; + snprintf(response, 256, + "HTTP/1.0 204 No Content\r\n" + "Access-Control-Allow-Origin: %s\r\n" + "\r\n", + ping_url_map_it->second.c_str()); + client->header_or_short_response = response; + } + + // Switch states. + client->state = Client::SENDING_SHORT_RESPONSE; + + epoll_event ev; + ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP; + ev.data.u64 = reinterpret_cast(client); + + if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) { + log_perror("epoll_ctl(EPOLL_CTL_MOD)"); + exit(1); + } +} + template void delete_from(vector *v, T elem) {