File: httpd.c

package info (click to toggle)
libdex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,136 kB
  • sloc: ansic: 13,114; asm: 226; makefile: 22; sh: 6; javascript: 5
file content (302 lines) | stat: -rw-r--r-- 9,397 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* httpd.c
 *
 * Copyright 2022 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <libdex.h>
#include <libsoup/soup.h>
#include <unistd.h>

static const char *errbody = "<html><body><h1>Not Found</h1></body></html>";

typedef struct
{
  SoupServer        *server;
  SoupServerMessage *message;
  char              *path;
  GHashTable        *query;
} Request;

static void
request_free (gpointer data)
{
  Request *request = data;

  g_clear_pointer (&request->path, g_free);
  g_clear_pointer (&request->query, g_hash_table_unref);
  g_clear_object (&request->message);
  g_clear_object (&request->server);
  g_free (request);
}

static void
request_pause (Request *request)
{
#if SOUP_CHECK_VERSION(3, 2, 0)
  soup_server_message_pause (request->message);
#else
  soup_server_pause_message (request->server, request->message);
#endif
}

static void
request_unpause (Request *request)
{
#if SOUP_CHECK_VERSION(3, 2, 0)
  soup_server_message_unpause (request->message);
#else
  soup_server_unpause_message (request->server, request->message);
#endif
}

static DexFuture *
request_fiber (gpointer user_data)
{
  Request *request = user_data;
  SoupMessageHeaders *headers;
  const char *path = request->path;
  GFileInfo *file_info;
  GError *error = NULL;
  GFile *file;
  GFileType file_type;

  while (path[0] == '/')
    path++;

  if (path[0] == 0)
    path = ".";

  file = g_file_new_for_path (path);
  file_info = dex_await_object (dex_file_query_info (file,
                                                     G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME","
                                                     G_FILE_ATTRIBUTE_STANDARD_TYPE","
                                                     G_FILE_ATTRIBUTE_STANDARD_SIZE",",
                                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                                     G_PRIORITY_DEFAULT),
                                &error);

  if (file_info != NULL)
    file_type = g_file_info_get_file_type (file_info);
  else
    file_type = G_FILE_TYPE_UNKNOWN;

  headers = soup_server_message_get_response_headers (request->message);

  if (soup_server_message_get_method (request->message) == SOUP_METHOD_HEAD)
    {
      char *length = g_strdup_printf ("%"G_GOFFSET_FORMAT, g_file_info_get_size (file_info));
      soup_message_headers_append (headers, "Content-Length", length);
      soup_server_message_set_status (request->message, SOUP_STATUS_OK, NULL);
      g_free (length);
      goto unpause;
    }

  /* TODO: It would be nice to use io_uring_prep_openat() via the
   * AIO backend here so that we can multiplex without hitting the
   * GIO async aio threadpool. That will require additions to
   * DexAioBackend to accomidate it, but would save us potentially
   * clogging the GIO pool as well as avoiding blocking on open()
   * in a fiber/worker.
   */

  if (file_type == G_FILE_TYPE_REGULAR)
    {
      SoupMessageBody *body = soup_server_message_get_response_body (request->message);
      goffset to_read = g_file_info_get_size (file_info);
      GInputStream *stream;
      gsize buflen = to_read;

      if (to_read > 4096*16)
        buflen = 1024*64 - 2*GLIB_SIZEOF_VOID_P;

      if (!(stream = dex_await_object (dex_file_read (file, G_PRIORITY_DEFAULT), &error)))
        goto respond_404;

      soup_message_headers_set_encoding (headers, SOUP_ENCODING_CHUNKED);
      soup_server_message_set_status (request->message, SOUP_STATUS_OK, NULL);

      for (;;)
        {
          guint8 *buffer = g_malloc (buflen);
          gssize len;

          len = dex_await_int64 (dex_input_stream_read (stream, buffer, MIN (to_read, buflen), G_PRIORITY_DEFAULT), &error);
          if (error != NULL || len <= 0)
            break;

          soup_message_body_append (body, SOUP_MEMORY_TAKE, buffer, len);

          to_read -= len;
          if (to_read <= 0)
            break;
        }

      soup_message_body_complete (body);
    }
  else if (file_type == G_FILE_TYPE_DIRECTORY)
    {
      GFileEnumerator *enumerator;
      GString *body;
      GList *files;
      gsize len;

      if (!(enumerator = dex_await_object (dex_file_enumerate_children (file,
                                                                        G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME","
                                                                        G_FILE_ATTRIBUTE_STANDARD_SIZE",",
                                                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                                                        G_PRIORITY_DEFAULT),
                                           &error)))
        goto respond_404;

      soup_server_message_set_status (request->message, SOUP_STATUS_OK, NULL);

      body = g_string_new (NULL);
      g_string_append_printf (body, "<html><body><h1>/%s</h1><ul>",
                              g_str_equal (path, ".") ? "" : path);

      while ((files = dex_await_boxed (dex_file_enumerator_next_files (enumerator, 100, G_PRIORITY_DEFAULT), &error)))
        {
          for (const GList *iter = files; iter; iter = iter->next)
            {
              GFileInfo *info = iter->data;
              goffset size = g_file_info_get_size (info);

              g_string_append_printf (body,
                                      "<li><a href=\"%s/%s\">%s%s</a> - %"G_GOFFSET_FORMAT"</li>\n",
                                      path,
                                      g_file_info_get_name (info),
                                      g_file_info_get_display_name (info),
                                      g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY ? "/" : "",
                                      size);

              g_object_unref (info);
            }

          g_list_free (files);
        }

      g_string_append (body, "</h1></body></html>");
      len = body->len;

      soup_server_message_set_response (request->message,
                                        "text/html",
                                        SOUP_MEMORY_TAKE,
                                        g_string_free (body, FALSE),
                                        len);
    }
  else
    {
respond_404:
      soup_server_message_set_response (request->message,
                                        "text/html",
                                        SOUP_MEMORY_COPY,
                                        errbody,
                                        strlen (errbody));
    }

unpause:
  request_unpause (request);

  g_clear_object (&file_info);
  g_clear_object (&file);
  g_clear_error (&error);

  return NULL;
}

static void
handle_message (SoupServer        *server,
                SoupServerMessage *message,
                const char        *path,
                GHashTable        *query,
                gpointer           user_data)
{
  Request *request;
  const char *method;

  g_assert (SOUP_IS_SERVER (server));
  g_assert (SOUP_IS_SERVER_MESSAGE (message));
  g_assert (path != NULL);

  method = soup_server_message_get_method (message);

  if (method != SOUP_METHOD_GET && method != SOUP_METHOD_HEAD)
    {
      soup_server_message_set_status (message, SOUP_STATUS_NOT_IMPLEMENTED, NULL);
      return;
    }

  request = g_new0 (Request, 1);
  request->server = g_object_ref (server);
  request->message = g_object_ref (message);
  request->path = g_strdup (path);
  request->query = query ? g_hash_table_ref (query) : NULL;

  request_pause (request);

  dex_future_disown (dex_scheduler_spawn (NULL, 0, request_fiber, request, request_free));
}

static void
print_uris (SoupServer *server)
{
  GSList *uris;

  g_assert (SOUP_IS_SERVER (server));

  if ((uris = soup_server_get_uris (server)))
    {
      for (const GSList *iter = uris; iter; iter = iter->next)
        {
          GUri *uri = iter->data;
          char *str = g_uri_to_string (uri);
          g_printerr ("Listening on %s\n", str);
          g_free (str);
        }

      g_slist_free_full (g_steal_pointer (&uris), (GDestroyNotify)g_uri_unref);
    }
}

int
main (int argc,
      char *argv[])
{
  SoupServer *server;
  GMainLoop *main_loop;
  GError *error = NULL;

  dex_init ();

  main_loop = g_main_loop_new (NULL, FALSE);
  server = soup_server_new ("server-header", "libdex-httpd",
                            NULL);
  soup_server_add_handler (server, "/", handle_message, NULL, NULL);
  if (!soup_server_listen_all (server, 8080, 0, &error))
    g_error ("%s", error->message);
  print_uris (server);

  g_main_loop_run (main_loop);

  g_main_loop_unref (main_loop);
  g_object_unref (server);

  return EXIT_SUCCESS;
}