]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Release Cubemap 1.2.2.
[cubemap] / server.cpp
index 615e8fb31d48b137df3cdcb3acf8caa4cbf00c68..9b83ddccd4fd0ff53f39bd08a702495312282d5b 100644 (file)
@@ -288,17 +288,17 @@ void Server::add_client_from_serialized(const ClientProto &client)
 
 int Server::lookup_stream_by_url(const string &url) const
 {
-       map<string, int>::const_iterator url_it = url_map.find(url);
-       if (url_it == url_map.end()) {
+       map<string, int>::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<string, int>::const_iterator url_map_it = url_map.find(url);
-       if (url_map_it == url_map.end()) {
-               return 404;  // Not found.
+       map<string, int>::const_iterator stream_url_map_it = stream_url_map.find(url);
+       if (stream_url_map_it == stream_url_map.end()) {
+               map<string, string>::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<string, string>::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<uint64_t>(client);
+
+       if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
+               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
+               exit(1);
+       }
+}
+
 template<class T>
 void delete_from(vector<T> *v, T elem)
 {