Skip to content

Commit

Permalink
Run clang-format on #604
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Oct 29, 2024
1 parent 108191a commit 267b8b5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
7 changes: 4 additions & 3 deletions llamafile/server/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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")
Expand All @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions llamafile/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -130,4 +131,4 @@ main(int argc, char* argv[])
while (!pthread_orphan_np())
pthread_decimate_np();
CheckForMemoryLeaks();
}
}
40 changes: 40 additions & 0 deletions llamafile/server/normalize_url_prefix.cpp
Original file line number Diff line number Diff line change
@@ -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 <cosmo.h>

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;
}
26 changes: 26 additions & 0 deletions llamafile/server/or_empty.cpp
Original file line number Diff line number Diff line change
@@ -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<ctl::string_view> x)
{
if (x.has_value())
return x.value();
return {};
}
28 changes: 4 additions & 24 deletions llamafile/server/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ctl::string_view> x)
{
if (x.has_value())
return x.value();
return {};
}
ctl::string_view
or_empty(ctl::optional<ctl::string_view> 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;
}
ctl::string
normalize_url_prefix(ctl::string url_prefix);

0 comments on commit 267b8b5

Please sign in to comment.