Skip to content

Commit

Permalink
Respond to HTTP OPTIONS requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 12, 2024
1 parent 28c8e22 commit 30518ca
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions llamafile/server/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,6 @@ Client::read_request()
bool
Client::transport()
{
if (msg_.version > 11) {
close_connection_ = true;
return send_error(505);
}

if (msg_.method == kHttpConnect) {
close_connection_ = true;
return send_error(501);
}

if (!has_at_most_this_element(kHttpExpect, "100-continue")) {
close_connection_ = true;
return send_error(417);
}

effective_ip_ = client_ip_;
effective_ip_trusted_ = client_ip_trusted_;
if (FLAG_ip_header) {
Expand Down Expand Up @@ -238,6 +223,21 @@ Client::transport()
}
}

if (msg_.version > 11) {
close_connection_ = true;
return send_error(505);
}

if (msg_.method == kHttpConnect) {
close_connection_ = true;
return send_error(501);
}

if (!has_at_most_this_element(kHttpExpect, "100-continue")) {
close_connection_ = true;
return send_error(417);
}

if (HasHeader(kHttpTransferEncoding))
if (!HeaderEqualCase(kHttpTransferEncoding, "identity")) {
close_connection_ = true;
Expand Down Expand Up @@ -614,6 +614,20 @@ Client::dispatch()
bool
Client::dispatcher()
{
if (msg_.method == kHttpOptions) {
char* p = obuf_.p;
char* headers = p;
p = append_http_response_message(p, 204);
p = stpcpy(p, "Accept: */*\r\n");
p = stpcpy(p, "Accept-Charset: utf-8\r\n");
p = stpcpy(p, "Allow: GET, POST, OPTIONS\r\n");
for (const auto& h : FLAG_headers) {
p = (char*)mempcpy(p, h.data(), h.size());
p = stpcpy(p, "\r\n");
}
return send_response(headers, p, "");
}

// get request-uri path
std::string_view p1 = path();
if (FLAG_verbose >= 2)
Expand Down

0 comments on commit 30518ca

Please sign in to comment.