+ size_t pos = url.find("?frag=");
+ if (pos != string::npos) {
+ // Parse an endpoint of the type /stream.mp4?frag=1234-5678.
+ const char *ptr = url.c_str() + pos + 6;
+
+ // "?frag=header" is special.
+ if (strcmp(ptr, "header") == 0) {
+ client->stream_pos = Client::STREAM_POS_HEADER_ONLY;
+ client->stream_pos_end = -1;
+ } else {
+ char *endptr;
+ long long frag_start = strtol(ptr, &endptr, 10);
+ if (ptr == endptr || frag_start < 0 || frag_start == LLONG_MAX) {
+ return 400; // Bad request.
+ }
+ if (*endptr != '-') {
+ return 400; // Bad request.
+ }
+ ptr = endptr + 1;
+
+ long long frag_end = strtol(ptr, &endptr, 10);
+ if (ptr == endptr || frag_end < frag_start || frag_end == LLONG_MAX) {
+ return 400; // Bad request.
+ }
+
+ if (*endptr != '\0') {
+ return 400; // Bad request.
+ }
+
+ client->stream_pos = frag_start;
+ client->stream_pos_end = frag_end;
+ }
+ url = url.substr(0, pos);
+ } else {
+ client->stream_pos = -1;
+ client->stream_pos_end = -1;
+ }
+ }
+
+ // Figure out if we're supposed to close the socket after we've delivered the response.
+ string protocol = request_tokens[2];
+ if (protocol.find("HTTP/") != 0) {
+ return 400; // Bad request.
+ }
+ client->close_after_response = false;
+ client->http_11 = true;
+ if (protocol == "HTTP/1.0") {
+ // No persistent connections.
+ client->close_after_response = true;
+ client->http_11 = false;
+ } else {
+ const auto connection_it = headers.find("Connection");
+ if (connection_it != headers.end() && connection_it->second == "close") {
+ client->close_after_response = true;
+ }