diff --git a/llamafile/server/client.cpp b/llamafile/server/client.cpp index 6c3caad81c..63617d4448 100644 --- a/llamafile/server/client.cpp +++ b/llamafile/server/client.cpp @@ -587,7 +587,7 @@ Client::dispatcher() if (!g_url_prefix.empty()) { if (FLAG_verbose >= 2) { - SLOG("request path %.*s", (int)p.size(), p.data()); + SLOG("request path %.*s", (int)p.size(), p.data()); } size_t prefix_len = g_url_prefix.size(); @@ -598,8 +598,7 @@ Client::dispatcher() } // Adjust path view to exclude prefix - p = ctl::string_view(p.data() + prefix_len, - p.size() - prefix_len); + p = ctl::string_view(p.data() + prefix_len, p.size() - prefix_len); } if (p == "/tokenize") @@ -610,6 +609,8 @@ Client::dispatcher() return embedding(); if (p == "/completion") return completion(); + + SLOG("path not found: %.*s", (int)p.size(), p.data()); return send_error(404); } diff --git a/llamafile/server/main.cpp b/llamafile/server/main.cpp index 080b675fce..ffa5d6f5b5 100644 --- a/llamafile/server/main.cpp +++ b/llamafile/server/main.cpp @@ -61,7 +61,8 @@ main(int argc, char* argv[]) llamafile_get_flags(argc, argv); // normalize URL prefix - g_url_prefix = normalize_url_prefix(FLAG_url_prefix); + if (FLAG_url_prefix) + g_url_prefix = normalize_url_prefix(FLAG_url_prefix); // initialize subsystems time_init(); @@ -130,4 +131,4 @@ main(int argc, char* argv[]) while (!pthread_orphan_np()) pthread_decimate_np(); CheckForMemoryLeaks(); -} \ No newline at end of file +} diff --git a/llamafile/server/normalize_url_prefix.cpp b/llamafile/server/normalize_url_prefix.cpp new file mode 100644 index 0000000000..6397d95e15 --- /dev/null +++ b/llamafile/server/normalize_url_prefix.cpp @@ -0,0 +1,40 @@ +// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*- +// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi +// +// Copyright 2024 Mozilla Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "utils.h" + +#include + +ctl::string +normalize_url_prefix(ctl::string url_prefix) +{ + // Rule 1: Replace multiple slashes with single slash + while (url_prefix.find("//") != ctl::string::npos) { + url_prefix.replace(url_prefix.find("//"), 2, "/"); + } + + // Rule 2: Remove trailing slash + if (!url_prefix.empty() && url_prefix.back() == '/') { + url_prefix.pop_back(); + } + + // Rule 3: Convert single slash to empty string + if (url_prefix == "/") { + url_prefix.clear(); + } + return url_prefix; +} diff --git a/llamafile/server/or_empty.cpp b/llamafile/server/or_empty.cpp new file mode 100644 index 0000000000..fbaa484d71 --- /dev/null +++ b/llamafile/server/or_empty.cpp @@ -0,0 +1,26 @@ +// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*- +// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi +// +// Copyright 2024 Mozilla Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "utils.h" + +ctl::string_view +or_empty(ctl::optional x) +{ + if (x.has_value()) + return x.value(); + return {}; +} diff --git a/llamafile/server/utils.h b/llamafile/server/utils.h index 4435138da4..9e1c3ae608 100644 --- a/llamafile/server/utils.h +++ b/llamafile/server/utils.h @@ -25,28 +25,8 @@ extern const signed char kHexToInt[256]; bool atob(ctl::string_view, bool); -static inline ctl::string_view -or_empty(ctl::optional x) -{ - if (x.has_value()) - return x.value(); - return {}; -} +ctl::string_view +or_empty(ctl::optional x); -static inline ctl::string normalize_url_prefix(ctl::string url_prefix) { - // Rule 1: Replace multiple slashes with single slash - while (url_prefix.find("//") != ctl::string::npos) { - url_prefix.replace(url_prefix.find("//"), 2, "/"); - } - - // Rule 2: Remove trailing slash - if (!url_prefix.empty() && url_prefix.back() == '/') { - url_prefix.pop_back(); - } - - // Rule 3: Convert single slash to empty string - if (url_prefix == "/") { - url_prefix.clear(); - } - return url_prefix; -} \ No newline at end of file +ctl::string +normalize_url_prefix(ctl::string url_prefix);