fetcher: add libsoup3 backend

The default is still soup2, you can use --with-soup3 to enable
the soup3 backend instead.
This commit is contained in:
Daniel Kolesa 2022-02-17 20:12:18 +01:00 committed by Dan Nicholson
parent 3ec7b5db18
commit d0ea2db430
9 changed files with 1544 additions and 73 deletions

View File

@ -221,18 +221,25 @@ libostree_1_la_SOURCES += \
$(NULL) $(NULL)
endif endif
# Only enable one fetcher backend.
if USE_CURL if USE_CURL
libostree_1_la_SOURCES += src/libostree/ostree-fetcher-curl.c \ libostree_1_la_SOURCES += src/libostree/ostree-fetcher-curl.c \
$(NULL) $(NULL)
libostree_1_la_CFLAGS += $(OT_DEP_CURL_CFLAGS) libostree_1_la_CFLAGS += $(OT_DEP_CURL_CFLAGS)
libostree_1_la_LIBADD += $(OT_DEP_CURL_LIBS) libostree_1_la_LIBADD += $(OT_DEP_CURL_LIBS)
else else
if USE_LIBSOUP3
libostree_1_la_SOURCES += src/libostree/ostree-fetcher-soup3.c
libostree_1_la_CFLAGS += $(OT_INTERNAL_SOUP_CFLAGS)
libostree_1_la_LIBADD += $(OT_INTERNAL_SOUP_LIBS)
else
if USE_LIBSOUP if USE_LIBSOUP
libostree_1_la_SOURCES += src/libostree/ostree-fetcher-soup.c libostree_1_la_SOURCES += src/libostree/ostree-fetcher-soup.c
libostree_1_la_CFLAGS += $(OT_INTERNAL_SOUP_CFLAGS) libostree_1_la_CFLAGS += $(OT_INTERNAL_SOUP_CFLAGS)
libostree_1_la_LIBADD += $(OT_INTERNAL_SOUP_LIBS) libostree_1_la_LIBADD += $(OT_INTERNAL_SOUP_LIBS)
endif endif
endif endif
endif
if USE_LIBMOUNT if USE_LIBMOUNT
libostree_1_la_CFLAGS += $(OT_DEP_LIBMOUNT_CFLAGS) libostree_1_la_CFLAGS += $(OT_DEP_LIBMOUNT_CFLAGS)

View File

@ -34,7 +34,7 @@ ostree-init.1 ostree-log.1 ostree-ls.1 ostree-prune.1 ostree-pull-local.1 \
ostree-pull.1 ostree-refs.1 ostree-remote.1 ostree-reset.1 \ ostree-pull.1 ostree-refs.1 ostree-remote.1 ostree-reset.1 \
ostree-rev-parse.1 ostree-show.1 ostree-sign.1 ostree-summary.1 \ ostree-rev-parse.1 ostree-show.1 ostree-sign.1 ostree-summary.1 \
ostree-static-delta.1 ostree-static-delta.1
if USE_LIBSOUP if USE_LIBSOUP_OR_LIBSOUP3
man1_files += ostree-trivial-httpd.1 man1_files += ostree-trivial-httpd.1
else else
# We still want to distribute the source, even if we are not building it # We still want to distribute the source, even if we are not building it

View File

@ -141,7 +141,7 @@ if USE_CURL_OR_SOUP
ostree_SOURCES += src/ostree/ot-builtin-pull.c ostree_SOURCES += src/ostree/ot-builtin-pull.c
endif endif
if USE_LIBSOUP if USE_LIBSOUP_OR_LIBSOUP3
# Eventually once we stop things from using this, we should support disabling this # Eventually once we stop things from using this, we should support disabling this
ostree_SOURCES += src/ostree/ot-builtin-trivial-httpd.c ostree_SOURCES += src/ostree/ot-builtin-trivial-httpd.c
pkglibexec_PROGRAMS += ostree-trivial-httpd pkglibexec_PROGRAMS += ostree-trivial-httpd

View File

@ -185,7 +185,7 @@ else
EXTRA_DIST += tests/test-rofiles-fuse.sh EXTRA_DIST += tests/test-rofiles-fuse.sh
endif endif
if USE_LIBSOUP if USE_LIBSOUP_OR_LIBSOUP3
_installed_or_uninstalled_test_scripts += tests/test-remote-cookies.sh _installed_or_uninstalled_test_scripts += tests/test-remote-cookies.sh
endif endif
@ -438,7 +438,7 @@ dist_test_scripts += $(_installed_or_uninstalled_test_scripts)
test_programs += $(_installed_or_uninstalled_test_programs) test_programs += $(_installed_or_uninstalled_test_programs)
endif endif
if !USE_LIBSOUP if !USE_LIBSOUP_OR_LIBSOUP3
no-soup-for-you-warning: no-soup-for-you-warning:
@echo "WARNING: $(PACKAGE) was built without libsoup, which is currently" 1>&2 @echo "WARNING: $(PACKAGE) was built without libsoup, which is currently" 1>&2
@echo "WARNING: required for many unit tests." 1>&2 @echo "WARNING: required for many unit tests." 1>&2

View File

@ -29,8 +29,7 @@ AM_CPPFLAGS += -DDATADIR='"$(datadir)"' -DLIBEXECDIR='"$(libexecdir)"' \
-DOSTREE_COMPILATION \ -DOSTREE_COMPILATION \
-DG_LOG_DOMAIN=\"OSTree\" \ -DG_LOG_DOMAIN=\"OSTree\" \
-DOSTREE_GITREV='"$(OSTREE_GITREV)"' \ -DOSTREE_GITREV='"$(OSTREE_GITREV)"' \
-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_66 '-DGLIB_VERSION_MAX_ALLOWED=G_ENCODE_VERSION(2,70)' \ -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_66 '-DGLIB_VERSION_MAX_ALLOWED=G_ENCODE_VERSION(2,70)'
-DSOUP_VERSION_MIN_REQUIRED=SOUP_VERSION_2_40 '-DSOUP_VERSION_MAX_ALLOWED=G_ENCODE_VERSION(2,48)'
# For strict aliasing, see https://bugzilla.gnome.org/show_bug.cgi?id=791622 # For strict aliasing, see https://bugzilla.gnome.org/show_bug.cgi?id=791622
AM_CFLAGS += -std=gnu99 -fno-strict-aliasing $(WARN_CFLAGS) AM_CFLAGS += -std=gnu99 -fno-strict-aliasing $(WARN_CFLAGS)
AM_DISTCHECK_CONFIGURE_FLAGS += \ AM_DISTCHECK_CONFIGURE_FLAGS += \
@ -39,6 +38,10 @@ AM_DISTCHECK_CONFIGURE_FLAGS += \
--disable-maintainer-mode \ --disable-maintainer-mode \
$(NULL) $(NULL)
if USE_LIBSOUP
AM_CPPFLAGS += -DSOUP_VERSION_MIN_REQUIRED=SOUP_VERSION_2_40 '-DSOUP_VERSION_MAX_ALLOWED=G_ENCODE_VERSION(2,48)'
endif
GITIGNOREFILES = aclocal.m4 build-aux/ buildutil/*.m4 config.h.in gtk-doc.make GITIGNOREFILES = aclocal.m4 build-aux/ buildutil/*.m4 config.h.in gtk-doc.make
# Generated by ci/gh-build.sh # Generated by ci/gh-build.sh
@ -69,8 +72,13 @@ EXTRA_DIST += autogen.sh COPYING README.md
OT_INTERNAL_GIO_UNIX_CFLAGS = $(OT_DEP_GIO_UNIX_CFLAGS) OT_INTERNAL_GIO_UNIX_CFLAGS = $(OT_DEP_GIO_UNIX_CFLAGS)
OT_INTERNAL_GIO_UNIX_LIBS = $(OT_DEP_GIO_UNIX_LIBS) OT_INTERNAL_GIO_UNIX_LIBS = $(OT_DEP_GIO_UNIX_LIBS)
if USE_LIBSOUP3
OT_INTERNAL_SOUP_CFLAGS = $(OT_DEP_SOUP3_CFLAGS)
OT_INTERNAL_SOUP_LIBS = $(OT_DEP_SOUP3_LIBS)
else
OT_INTERNAL_SOUP_CFLAGS = $(OT_DEP_SOUP_CFLAGS) OT_INTERNAL_SOUP_CFLAGS = $(OT_DEP_SOUP_CFLAGS)
OT_INTERNAL_SOUP_LIBS = $(OT_DEP_SOUP_LIBS) OT_INTERNAL_SOUP_LIBS = $(OT_DEP_SOUP_LIBS)
endif
# This canonicalizes the PKG_CHECK_MODULES or AM_PATH_GPGME results # This canonicalizes the PKG_CHECK_MODULES or AM_PATH_GPGME results
if USE_GPGME if USE_GPGME

View File

@ -149,6 +149,21 @@ AS_IF([test x$enable_http2 != xno ], [
OSTREE_FEATURES="$OSTREE_FEATURES no-http2" OSTREE_FEATURES="$OSTREE_FEATURES no-http2"
]) ])
SOUP3_DEPENDENCY="libsoup-3.0 >= 3.0.0"
AC_ARG_WITH(soup3,
AS_HELP_STRING([--with-soup3], [Use libsoup3 @<:@default=no@:>@]),
[], [with_soup3=no])
AS_IF([test x$with_soup3 != xno], [
PKG_CHECK_MODULES(OT_DEP_SOUP3, $SOUP3_DEPENDENCY)
with_soup3=yes
AC_DEFINE([HAVE_LIBSOUP3], 1, [Define if we have libsoup3])
OSTREE_FEATURES="$OSTREE_FEATURES libsoup3"
with_soup_default=no
dnl soup3 always supports client certs
have_libsoup_client_certs=yes
], [with_soup_default=check])
AM_CONDITIONAL(USE_LIBSOUP3, test x$with_soup3 != xno)
dnl When bumping the libsoup-2.4 dependency, remember to bump dnl When bumping the libsoup-2.4 dependency, remember to bump
dnl SOUP_VERSION_MIN_REQUIRED and SOUP_VERSION_MAX_ALLOWED in dnl SOUP_VERSION_MIN_REQUIRED and SOUP_VERSION_MAX_ALLOWED in
dnl Makefile.am dnl Makefile.am
@ -190,6 +205,13 @@ if test x$with_soup != xno; then OSTREE_FEATURES="$OSTREE_FEATURES libsoup"; fi
AM_CONDITIONAL(USE_LIBSOUP, test x$with_soup != xno) AM_CONDITIONAL(USE_LIBSOUP, test x$with_soup != xno)
AM_CONDITIONAL(HAVE_LIBSOUP_CLIENT_CERTS, test x$have_libsoup_client_certs = xyes) AM_CONDITIONAL(HAVE_LIBSOUP_CLIENT_CERTS, test x$have_libsoup_client_certs = xyes)
dnl Some components use either soup2 or soup3.
AM_CONDITIONAL([USE_LIBSOUP_OR_LIBSOUP3],
[test x$with_soup = xyes || test x$with_soup3 = xyes])
AS_IF([test x$with_soup = xyes || test x$with_soup3 = xyes], [
AC_DEFINE([HAVE_LIBSOUP_OR_LIBSOUP3], 1, [Define if we have libsoup.pc or libsoup3.pc])
])
AC_ARG_ENABLE(trivial-httpd-cmdline, AC_ARG_ENABLE(trivial-httpd-cmdline,
[AS_HELP_STRING([--enable-trivial-httpd-cmdline], [AS_HELP_STRING([--enable-trivial-httpd-cmdline],
[Continue to support "ostree trivial-httpd" [default=no]])],, [Continue to support "ostree trivial-httpd" [default=no]])],,
@ -198,13 +220,16 @@ AS_IF([test x$enable_trivial_httpd_cmdline = xyes],
[AC_DEFINE([BUILDOPT_ENABLE_TRIVIAL_HTTPD_CMDLINE], 1, [Define if we are enabling ostree trivial-httpd entrypoint])] [AC_DEFINE([BUILDOPT_ENABLE_TRIVIAL_HTTPD_CMDLINE], 1, [Define if we are enabling ostree trivial-httpd entrypoint])]
) )
AS_IF([test x$with_curl = xyes && test x$with_soup = xno], [ AS_IF([test x$with_curl = xyes && test x$with_soup = xno && test x$with_soup3 = xno], [
AC_MSG_WARN([Curl enabled, but libsoup is not; libsoup is needed for tests (make check, etc.)]) AC_MSG_WARN([Curl enabled, but libsoup is not; libsoup is needed for tests (make check, etc.)])
]) ])
AM_CONDITIONAL(USE_CURL_OR_SOUP, test x$with_curl != xno || test x$with_soup != xno) AM_CONDITIONAL(USE_CURL_OR_SOUP, test x$with_curl != xno || test x$with_soup != xno || test x$with_soup3 != xno)
AS_IF([test x$with_curl != xno || test x$with_soup != xno], AS_IF([test x$with_curl != xno || test x$with_soup != xno || test x$with_soup3 != xno],
[AC_DEFINE([HAVE_LIBCURL_OR_LIBSOUP], 1, [Define if we have soup or curl])]) [AC_DEFINE([HAVE_LIBCURL_OR_LIBSOUP], 1, [Define if we have soup or curl])])
AS_IF([test x$with_curl = xyes], [fetcher_backend=curl], [test x$with_soup = xyes], [fetcher_backend=libsoup], [fetcher_backend=none]) AS_IF([test x$with_curl = xyes], [fetcher_backend=curl],
[test x$with_soup = xyes], [fetcher_backend=libsoup],
[test x$with_soup3 = xyes], [fetcher_backend=libsoup3],
[fetcher_backend=none])
m4_ifdef([GOBJECT_INTROSPECTION_CHECK], [ m4_ifdef([GOBJECT_INTROSPECTION_CHECK], [
GOBJECT_INTROSPECTION_CHECK([1.51.5]) GOBJECT_INTROSPECTION_CHECK([1.51.5])

File diff suppressed because it is too large Load Diff

View File

@ -118,7 +118,7 @@ static OstreeCommand commands[] = {
{ "summary", OSTREE_BUILTIN_FLAG_NONE, { "summary", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_summary, ostree_builtin_summary,
"Manage summary metadata" }, "Manage summary metadata" },
#if defined(HAVE_LIBSOUP) && defined(BUILDOPT_ENABLE_TRIVIAL_HTTPD_CMDLINE) #if defined(HAVE_LIBSOUP_OR_LIBSOUP3) && defined(BUILDOPT_ENABLE_TRIVIAL_HTTPD_CMDLINE)
{ "trivial-httpd", OSTREE_BUILTIN_FLAG_NONE, { "trivial-httpd", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_trivial_httpd, ostree_builtin_trivial_httpd,
NULL }, NULL },

View File

@ -34,6 +34,19 @@
#include <sys/prctl.h> #include <sys/prctl.h>
#include <signal.h> #include <signal.h>
#if ! SOUP_CHECK_VERSION (3, 0, 0)
# define SoupServerMessage SoupMessage
# define soup_server_message_get_method(msg) ((msg)->method)
# define soup_server_message_get_request_headers(msg) ((msg)->request_headers)
# define soup_server_message_get_response_headers(msg) ((msg)->response_headers)
# define soup_server_message_get_response_body(msg) ((msg)->response_body)
# define soup_server_message_set_status(msg, status) soup_message_set_status(msg, status)
# define soup_server_message_set_redirect(msg, status, uri) soup_message_set_redirect(msg, status, uri)
# define soup_server_message_set_response(msg, ct, ru, rb, rl) soup_message_set_response(msg, ct, ru, rb, rl)
#else
# define soup_server_message_set_status(msg, status) soup_server_message_set_status(msg, status, NULL)
#endif
static char *opt_port_file = NULL; static char *opt_port_file = NULL;
static char *opt_log = NULL; static char *opt_log = NULL;
static gboolean opt_daemonize; static gboolean opt_daemonize;
@ -188,15 +201,12 @@ is_safe_to_access (struct stat *stbuf)
} }
static void static void
close_socket (SoupMessage *msg, gpointer user_data) close_socket (SoupServerMessage *msg, gpointer user_data)
{ {
SoupSocket *sock = user_data; GSocket *sock = user_data;
int sockfd; int sockfd;
/* Actually calling soup_socket_disconnect() here would cause sockfd = g_socket_get_fd (sock);
* us to leak memory, so just shutdown the socket instead.
*/
sockfd = soup_socket_get_fd (sock);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
shutdown (sockfd, SD_SEND); shutdown (sockfd, SD_SEND);
#else #else
@ -213,12 +223,55 @@ calculate_etag (GMappedFile *mapping)
return g_strconcat ("\"", checksum, "\"", NULL); return g_strconcat ("\"", checksum, "\"", NULL);
} }
static GSList *
_server_cookies_from_request (SoupServerMessage *msg)
{
SoupCookie *cookie;
GSList *cookies = NULL;
GHashTable *params;
GHashTableIter iter;
gpointer name, value;
const char *header;
const char *host;
header = soup_message_headers_get_one (soup_server_message_get_request_headers (msg),
"Cookie");
if (!header)
return NULL;
#if ! SOUP_CHECK_VERSION (3, 0, 0)
host = soup_uri_get_host (soup_message_get_uri (msg));
#else
host = g_uri_get_host (soup_server_message_get_uri (msg));
#endif
params = soup_header_parse_semi_param_list (header);
g_hash_table_iter_init (&iter, params);
while (g_hash_table_iter_next (&iter, &name, &value))
{
if (!name || !value) continue;
cookie = soup_cookie_new (name, value, host, NULL, 0);
cookies = g_slist_prepend (cookies, cookie);
}
soup_header_free_param_list (params);
return g_slist_reverse (cookies);
}
static void static void
#if ! SOUP_CHECK_VERSION (3, 0, 0)
do_get (OtTrivialHttpd *self, do_get (OtTrivialHttpd *self,
SoupServer *server, SoupServer *server,
SoupMessage *msg, SoupServerMessage *msg,
const char *path, const char *path,
SoupClientContext *context) SoupClientContext *context)
#else
do_get (OtTrivialHttpd *self,
SoupServer *server,
SoupServerMessage *msg,
const char *path)
#endif
{ {
char *slash; char *slash;
int ret; int ret;
@ -228,7 +281,7 @@ do_get (OtTrivialHttpd *self,
if (opt_expected_cookies) if (opt_expected_cookies)
{ {
GSList *cookies = soup_cookies_from_request (msg); GSList *cookies = _server_cookies_from_request (msg);
GSList *l; GSList *l;
int i; int i;
@ -253,12 +306,12 @@ do_get (OtTrivialHttpd *self,
if (!found) if (!found)
{ {
httpd_log (self, "Expected cookie not found %s\n", k); httpd_log (self, "Expected cookie not found %s\n", k);
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
soup_cookies_free (cookies); g_slist_free_full (cookies, (GDestroyNotify)soup_cookie_free);
goto out; goto out;
} }
} }
soup_cookies_free (cookies); g_slist_free_full (cookies, (GDestroyNotify)soup_cookie_free);
} }
if (opt_expected_headers) if (opt_expected_headers)
@ -273,18 +326,18 @@ do_get (OtTrivialHttpd *self,
{ {
g_autofree char *k = g_strndup (kv, eq - kv); g_autofree char *k = g_strndup (kv, eq - kv);
const gchar *expected_v = eq + 1; const gchar *expected_v = eq + 1;
const gchar *found_v = soup_message_headers_get_one (msg->request_headers, k); const gchar *found_v = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), k);
if (!found_v) if (!found_v)
{ {
httpd_log (self, "Expected header not found %s\n", k); httpd_log (self, "Expected header not found %s\n", k);
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
goto out; goto out;
} }
if (strcmp (found_v, expected_v) != 0) if (strcmp (found_v, expected_v) != 0)
{ {
httpd_log (self, "Expected header %s: %s but found %s\n", k, expected_v, found_v); httpd_log (self, "Expected header %s: %s but found %s\n", k, expected_v, found_v);
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
goto out; goto out;
} }
} }
@ -293,7 +346,7 @@ do_get (OtTrivialHttpd *self,
if (strstr (path, "../") != NULL) if (strstr (path, "../") != NULL)
{ {
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
goto out; goto out;
} }
@ -302,7 +355,7 @@ do_get (OtTrivialHttpd *self,
g_random_int_range (0, 100) < opt_random_500s_percentage) g_random_int_range (0, 100) < opt_random_500s_percentage)
{ {
emitted_random_500s_count++; emitted_random_500s_count++;
soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
goto out; goto out;
} }
else if (opt_random_408s_percentage > 0 && else if (opt_random_408s_percentage > 0 &&
@ -310,7 +363,7 @@ do_get (OtTrivialHttpd *self,
g_random_int_range (0, 100) < opt_random_408s_percentage) g_random_int_range (0, 100) < opt_random_408s_percentage)
{ {
emitted_random_408s_count++; emitted_random_408s_count++;
soup_message_set_status (msg, SOUP_STATUS_REQUEST_TIMEOUT); soup_server_message_set_status (msg, SOUP_STATUS_REQUEST_TIMEOUT);
goto out; goto out;
} }
@ -323,17 +376,17 @@ do_get (OtTrivialHttpd *self,
if (ret == -1) if (ret == -1)
{ {
if (errno == EPERM) if (errno == EPERM)
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
else if (errno == ENOENT) else if (errno == ENOENT)
soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND); soup_server_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
else else
soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
goto out; goto out;
} }
if (!is_safe_to_access (&stbuf)) if (!is_safe_to_access (&stbuf))
{ {
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
goto out; goto out;
} }
@ -344,8 +397,12 @@ do_get (OtTrivialHttpd *self,
{ {
g_autofree char *redir_uri = NULL; g_autofree char *redir_uri = NULL;
redir_uri = g_strdup_printf ("%s/", soup_message_get_uri (msg)->path); #if ! SOUP_CHECK_VERSION (3, 0, 0)
soup_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY, redir_uri = g_strdup_printf ("%s/", soup_uri_get_path (soup_message_get_uri (msg)));
#else
redir_uri = g_strdup_printf ("%s/", g_uri_get_path (soup_server_message_get_uri (msg)));
#endif
soup_server_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY,
redir_uri); redir_uri);
} }
else else
@ -354,15 +411,19 @@ do_get (OtTrivialHttpd *self,
if (fstatat (self->root_dfd, index_realpath, &stbuf, 0) != -1) if (fstatat (self->root_dfd, index_realpath, &stbuf, 0) != -1)
{ {
g_autofree char *index_path = g_strconcat (path, "/index.html", NULL); g_autofree char *index_path = g_strconcat (path, "/index.html", NULL);
#if ! SOUP_CHECK_VERSION (3, 0, 0)
do_get (self, server, msg, index_path, context); do_get (self, server, msg, index_path, context);
#else
do_get (self, server, msg, index_path);
#endif
} }
else else
{ {
GString *listing = get_directory_listing (self->root_dfd, path); GString *listing = get_directory_listing (self->root_dfd, path);
soup_message_set_response (msg, "text/html", soup_server_message_set_response (msg, "text/html",
SOUP_MEMORY_TAKE, SOUP_MEMORY_TAKE,
listing->str, listing->len); listing->str, listing->len);
soup_message_set_status (msg, SOUP_STATUS_OK); soup_server_message_set_status (msg, SOUP_STATUS_OK);
g_string_free (listing, FALSE); g_string_free (listing, FALSE);
} }
} }
@ -371,21 +432,21 @@ do_get (OtTrivialHttpd *self,
{ {
if (!S_ISREG (stbuf.st_mode)) if (!S_ISREG (stbuf.st_mode))
{ {
soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
goto out; goto out;
} }
glnx_autofd int fd = openat (self->root_dfd, path, O_RDONLY | O_CLOEXEC); glnx_autofd int fd = openat (self->root_dfd, path, O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
{ {
soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
goto out; goto out;
} }
g_autoptr(GMappedFile) mapping = g_mapped_file_new_from_fd (fd, FALSE, NULL); g_autoptr(GMappedFile) mapping = g_mapped_file_new_from_fd (fd, FALSE, NULL);
if (!mapping) if (!mapping)
{ {
soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
goto out; goto out;
} }
(void) close (fd); fd = -1; (void) close (fd); fd = -1;
@ -395,14 +456,14 @@ do_get (OtTrivialHttpd *self,
if (last_modified != NULL) if (last_modified != NULL)
{ {
g_autofree gchar *formatted = g_date_time_format (last_modified, "%a, %d %b %Y %H:%M:%S GMT"); g_autofree gchar *formatted = g_date_time_format (last_modified, "%a, %d %b %Y %H:%M:%S GMT");
soup_message_headers_append (msg->response_headers, "Last-Modified", formatted); soup_message_headers_append (soup_server_message_get_response_headers (msg), "Last-Modified", formatted);
} }
g_autofree gchar *etag = calculate_etag (mapping); g_autofree gchar *etag = calculate_etag (mapping);
if (etag != NULL) if (etag != NULL)
soup_message_headers_append (msg->response_headers, "ETag", etag); soup_message_headers_append (soup_server_message_get_response_headers (msg), "ETag", etag);
if (msg->method == SOUP_METHOD_GET) if (!strcmp (soup_server_message_get_method (msg), "GET"))
{ {
gsize buffer_length, file_size; gsize buffer_length, file_size;
SoupRange *ranges; SoupRange *ranges;
@ -410,13 +471,13 @@ do_get (OtTrivialHttpd *self,
gboolean have_ranges; gboolean have_ranges;
file_size = g_mapped_file_get_length (mapping); file_size = g_mapped_file_get_length (mapping);
have_ranges = soup_message_headers_get_ranges(msg->request_headers, file_size, &ranges, &ranges_length); have_ranges = soup_message_headers_get_ranges(soup_server_message_get_request_headers (msg), file_size, &ranges, &ranges_length);
if (opt_force_ranges && !have_ranges && g_strrstr (path, "/objects") != NULL) if (opt_force_ranges && !have_ranges && g_strrstr (path, "/objects") != NULL)
{ {
SoupSocket *sock; GSocket *sock;
buffer_length = file_size/2; buffer_length = file_size/2;
soup_message_headers_set_content_length (msg->response_headers, file_size); soup_message_headers_set_content_length (soup_server_message_get_response_headers (msg), file_size);
soup_message_headers_append (msg->response_headers, soup_message_headers_append (soup_server_message_get_response_headers (msg),
"Connection", "close"); "Connection", "close");
/* soup-message-io will wait for us to add /* soup-message-io will wait for us to add
@ -424,7 +485,11 @@ do_get (OtTrivialHttpd *self,
* the declared Content-Length. Instead, we * the declared Content-Length. Instead, we
* forcibly close the socket at that point. * forcibly close the socket at that point.
*/ */
sock = soup_client_context_get_socket (context); #if ! SOUP_CHECK_VERSION (3, 0, 0)
sock = soup_client_context_get_gsocket (context);
#else
sock = soup_server_message_get_socket (msg);
#endif
g_signal_connect (msg, "wrote-chunk", G_CALLBACK (close_socket), sock); g_signal_connect (msg, "wrote-chunk", G_CALLBACK (close_socket), sock);
} }
else else
@ -434,12 +499,13 @@ do_get (OtTrivialHttpd *self,
{ {
if (ranges_length > 0 && ranges[0].start >= file_size) if (ranges_length > 0 && ranges[0].start >= file_size)
{ {
soup_message_set_status (msg, SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE); soup_server_message_set_status (msg, SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE);
soup_message_headers_free_ranges (msg->request_headers, ranges); soup_message_headers_free_ranges (soup_server_message_get_request_headers (msg), ranges);
goto out; goto out;
} }
soup_message_headers_free_ranges (msg->request_headers, ranges); soup_message_headers_free_ranges (soup_server_message_get_request_headers (msg), ranges);
} }
#if ! SOUP_CHECK_VERSION (3, 0, 0)
if (buffer_length > 0) if (buffer_length > 0)
{ {
SoupBuffer *buffer; SoupBuffer *buffer;
@ -451,8 +517,22 @@ do_get (OtTrivialHttpd *self,
soup_message_body_append_buffer (msg->response_body, buffer); soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer); soup_buffer_free (buffer);
} }
#else
if (buffer_length > 0 && buffer_length == file_size)
{
GBytes *bytes = g_mapped_file_get_bytes (mapping);
soup_message_body_append_bytes (soup_server_message_get_response_body (msg), bytes);
g_bytes_unref (bytes);
} }
else /* msg->method == SOUP_METHOD_HEAD */ else if (buffer_length > 0)
{
gchar *contents = g_mapped_file_get_contents (mapping);
soup_message_body_append (soup_server_message_get_response_body (msg),
SOUP_MEMORY_COPY, contents, buffer_length);
}
#endif
}
else /* method == HEAD */
{ {
g_autofree char *length = NULL; g_autofree char *length = NULL;
@ -461,56 +541,59 @@ do_get (OtTrivialHttpd *self,
* But we'll optimize and avoid the extra I/O. * But we'll optimize and avoid the extra I/O.
*/ */
length = g_strdup_printf ("%lu", (gulong)stbuf.st_size); length = g_strdup_printf ("%lu", (gulong)stbuf.st_size);
soup_message_headers_append (msg->response_headers, soup_message_headers_append (soup_server_message_get_response_headers (msg),
"Content-Length", length); "Content-Length", length);
} }
/* Check clients caching headers. */ /* Check clients caching headers. */
const gchar *if_modified_since = soup_message_headers_get_one (msg->request_headers, const gchar *if_modified_since = soup_message_headers_get_one (soup_server_message_get_request_headers (msg),
"If-Modified-Since"); "If-Modified-Since");
const gchar *if_none_match = soup_message_headers_get_one (msg->request_headers, const gchar *if_none_match = soup_message_headers_get_one (soup_server_message_get_request_headers (msg),
"If-None-Match"); "If-None-Match");
if (if_none_match != NULL && etag != NULL) if (if_none_match != NULL && etag != NULL)
{ {
if (g_strcmp0 (etag, if_none_match) == 0) if (g_strcmp0 (etag, if_none_match) == 0)
{ {
soup_message_set_status (msg, SOUP_STATUS_NOT_MODIFIED); soup_server_message_set_status (msg, SOUP_STATUS_NOT_MODIFIED);
soup_message_body_truncate (msg->response_body); soup_message_body_truncate (soup_server_message_get_response_body (msg));
} }
else else
{ {
soup_message_set_status (msg, SOUP_STATUS_OK); soup_server_message_set_status (msg, SOUP_STATUS_OK);
} }
} }
else if (if_modified_since != NULL && last_modified != NULL) else if (if_modified_since != NULL && last_modified != NULL)
{ {
SoupDate *if_modified_since_sd = soup_date_new_from_string (if_modified_since);
g_autoptr(GDateTime) if_modified_since_dt = NULL; g_autoptr(GDateTime) if_modified_since_dt = NULL;
#if ! SOUP_CHECK_VERSION (3, 0, 0)
SoupDate *if_modified_since_sd = soup_date_new_from_string (if_modified_since);
if (if_modified_since_sd != NULL) if (if_modified_since_sd != NULL)
if_modified_since_dt = g_date_time_new_from_unix_utc (soup_date_to_time_t (if_modified_since_sd)); if_modified_since_dt = g_date_time_new_from_unix_utc (soup_date_to_time_t (if_modified_since_sd));
#else
if_modified_since_dt = soup_date_time_new_from_http_string (if_modified_since);
#endif
if (if_modified_since_dt != NULL && if (if_modified_since_dt != NULL &&
g_date_time_compare (last_modified, if_modified_since_dt) <= 0) g_date_time_compare (last_modified, if_modified_since_dt) <= 0)
{ {
soup_message_set_status (msg, SOUP_STATUS_NOT_MODIFIED); soup_server_message_set_status (msg, SOUP_STATUS_NOT_MODIFIED);
soup_message_body_truncate (msg->response_body); soup_message_body_truncate (soup_server_message_get_response_body (msg));
} }
else else
{ {
soup_message_set_status (msg, SOUP_STATUS_OK); soup_server_message_set_status (msg, SOUP_STATUS_OK);
} }
g_clear_pointer (&if_modified_since_sd, soup_date_free);
} }
else else
{ {
soup_message_set_status (msg, SOUP_STATUS_OK); soup_server_message_set_status (msg, SOUP_STATUS_OK);
} }
} }
out: out:
{ {
#if ! SOUP_CHECK_VERSION (3, 0, 0)
guint status = 0; guint status = 0;
g_autofree gchar *reason = NULL; g_autofree gchar *reason = NULL;
@ -518,26 +601,41 @@ do_get (OtTrivialHttpd *self,
"status-code", &status, "status-code", &status,
"reason-phrase", &reason, "reason-phrase", &reason,
NULL); NULL);
#else
guint status = soup_server_message_get_status (msg);
const char *reason = soup_server_message_get_reason_phrase (msg);
#endif
httpd_log (self, " status: %s (%u)\n", reason, status); httpd_log (self, " status: %s (%u)\n", reason, status);
} }
return; return;
} }
static void static void
httpd_callback (SoupServer *server, SoupMessage *msg, #if ! SOUP_CHECK_VERSION (3, 0, 0)
httpd_callback (SoupServer *server, SoupServerMessage *msg,
const char *path, GHashTable *query, const char *path, GHashTable *query,
SoupClientContext *context, gpointer data) SoupClientContext *context, gpointer data)
#else
httpd_callback (SoupServer *server, SoupServerMessage *msg,
const char *path, GHashTable *query, gpointer data)
#endif
{ {
OtTrivialHttpd *self = data; OtTrivialHttpd *self = data;
const char *meth = soup_server_message_get_method (msg);
if (msg->method == SOUP_METHOD_GET || msg->method == SOUP_METHOD_HEAD) if (!strcmp (meth, "GET") || !strcmp(meth, "HEAD"))
#if ! SOUP_CHECK_VERSION (3, 0, 0)
do_get (self, server, msg, path, context); do_get (self, server, msg, path, context);
#else
do_get (self, server, msg, path);
#endif
else else
soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED); soup_server_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
} }
static gboolean static gboolean
basic_auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg, basic_auth_callback (SoupAuthDomain *auth_domain, SoupServerMessage *msg,
const char *username, const char *password, gpointer data) const char *username, const char *password, gpointer data)
{ {
return g_str_equal (username, "foouser") && g_str_equal (password, "barpw"); return g_str_equal (username, "foouser") && g_str_equal (password, "barpw");
@ -703,7 +801,7 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error)
} }
#if SOUP_CHECK_VERSION(2, 48, 0) #if SOUP_CHECK_VERSION(2, 48, 0)
server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "ostree-httpd ", NULL); server = soup_server_new ("server-header", "ostree-httpd ", NULL);
if (!soup_server_listen_all (server, opt_port, 0, error)) if (!soup_server_listen_all (server, opt_port, 0, error))
goto out; goto out;
#else #else
@ -711,13 +809,21 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error)
SOUP_SERVER_SERVER_HEADER, "ostree-httpd ", SOUP_SERVER_SERVER_HEADER, "ostree-httpd ",
NULL); NULL);
#endif #endif
if (opt_require_basic_auth) if (opt_require_basic_auth)
{ {
#if ! SOUP_CHECK_VERSION (3, 0, 0)
glnx_unref_object SoupAuthDomain *auth_domain = glnx_unref_object SoupAuthDomain *auth_domain =
soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM, "auth-test", soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM, "auth-test",
SOUP_AUTH_DOMAIN_ADD_PATH, "/", SOUP_AUTH_DOMAIN_ADD_PATH, "/",
SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback, SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback,
NULL); NULL);
#else
glnx_unref_object SoupAuthDomain *auth_domain =
soup_auth_domain_basic_new ("realm", "auth-test", NULL);
soup_auth_domain_add_path (auth_domain, "/");
soup_auth_domain_basic_set_auth_callback (auth_domain, basic_auth_callback, NULL, NULL);
#endif
soup_server_add_auth_domain (server, auth_domain); soup_server_add_auth_domain (server, auth_domain);
} }