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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2001-2003, Ximian, Inc.
*/
#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <libsoup/soup.h>
#include <glib/gstdio.h>
#include <govirt/govirt.h>
#include "mock-httpd.h"
#define GOVIRT_HTTPS_PORT 8088
static GMutex run_mutex;
struct GovirtMockHttpd {
GThread *thread;
GMainLoop *loop;
guint port;
gboolean disable_tls;
GHashTable *requests;
};
typedef struct {
char *method;
char *path;
char *content;
} GovirtMockHttpdRequest;
static const char *
govirt_mock_httpd_find_request (GovirtMockHttpd *mock_httpd,
const char *method,
const char *path)
{
GovirtMockHttpdRequest *request;
request = g_hash_table_lookup (mock_httpd->requests, path);
if (request == NULL) {
return NULL;
}
if (g_strcmp0 (request->method, method) != 0) {
return NULL;
}
return request->content;
}
static void
govirt_mock_htttpd_request_free (GovirtMockHttpdRequest *request)
{
g_free (request->method);
g_free (request->path);
g_free (request->content);
g_free (request);
}
static void
server_callback (SoupServer *server, SoupServerMessage *msg,
const char *path, GHashTable *query,
gpointer data)
{
SoupMessageHeadersIter iter;
const char *name, *value;
const char *content;
GovirtMockHttpd *mock_httpd = data;
g_debug ("%s %s HTTP/1.%d", soup_server_message_get_method(msg), path,
soup_server_message_get_http_version (msg));
soup_message_headers_iter_init (&iter, soup_server_message_get_request_headers(msg));
while (soup_message_headers_iter_next (&iter, &name, &value))
g_debug ("%s: %s", name, value);
if (soup_server_message_get_request_body(msg)->length)
g_debug ("%s", soup_server_message_get_request_body(msg)->data);
content = govirt_mock_httpd_find_request(mock_httpd, soup_server_message_get_method(msg), path);
if (content == NULL) {
soup_server_message_set_status (msg, SOUP_STATUS_NOT_FOUND, NULL);
} else {
soup_message_body_append (soup_server_message_get_response_body(msg), SOUP_MEMORY_STATIC,
content, strlen(content));
soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
}
g_debug (" -> %d %s\n\n",
soup_server_message_get_status(msg),
soup_server_message_get_reason_phrase(msg));
}
static gpointer
govirt_httpd_run (gpointer user_data)
{
GovirtMockHttpd *mock_httpd = user_data;
GSList *uris, *u;
SoupServer *server;
GError *error = NULL;
GMainContext *context;
context = g_main_loop_get_context (mock_httpd->loop);
g_main_context_push_thread_default (context);
if (!mock_httpd->disable_tls) {
GTlsCertificate *cert;
cert = g_tls_certificate_new_from_files (abs_srcdir "/https-cert/server-cert.pem",
abs_srcdir "/https-cert/server-key.pem",
&error);
g_assert_no_error (error);
server = soup_server_new ("server-header", "simple-soup-httpd ",
"tls-certificate", cert,
NULL);
g_object_unref (cert);
soup_server_listen_local (server, mock_httpd->port,
SOUP_SERVER_LISTEN_HTTPS, &error);
} else {
server = soup_server_new ("server-header", "simple-soup-httpd ",
NULL);
soup_server_listen_local (server, mock_httpd->port, 0, &error);
}
g_assert_no_error (error);
soup_server_add_handler (server, NULL,
server_callback, mock_httpd, NULL);
uris = soup_server_get_uris (server);
for (u = uris; u; u = u->next) {
char *str;
str = g_uri_to_string (u->data);
g_debug ("Listening on %s", str);
g_free (str);
g_uri_unref (u->data);
}
g_slist_free (uris);
g_debug ("Waiting for requests...\n");
g_mutex_unlock(&run_mutex);
g_main_loop_run (mock_httpd->loop);
g_main_context_pop_thread_default (context);
g_object_unref (server);
return NULL;
}
GovirtMockHttpd *
govirt_mock_httpd_new (guint port)
{
GMainContext *context;
GovirtMockHttpd *mock_httpd;
mock_httpd = g_new0 (GovirtMockHttpd, 1);
mock_httpd->port = port;
context = g_main_context_new ();
mock_httpd->loop = g_main_loop_new (context, TRUE);
g_main_context_unref (context);
g_mutex_lock(&run_mutex);
mock_httpd->requests = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL,
(GDestroyNotify) govirt_mock_htttpd_request_free);
return mock_httpd;
}
void
govirt_mock_httpd_disable_tls (GovirtMockHttpd *mock_httpd, gboolean disable_tls)
{
g_return_if_fail(mock_httpd->thread == NULL);
mock_httpd->disable_tls = disable_tls;
}
void
govirt_mock_httpd_add_request (GovirtMockHttpd *mock_httpd,
const char *method,
const char *path,
const char *content)
{
GovirtMockHttpdRequest *request;
g_return_if_fail(mock_httpd->thread == NULL);
/* FIXME: just one method is supported for a given path right now */
request = g_hash_table_lookup (mock_httpd->requests, path);
if (request != NULL) {
g_assert (g_strcmp0 (request->method, method) == 0);
}
request = g_new0 (GovirtMockHttpdRequest, 1);
request->method = g_strdup (method);
request->path = g_strdup (path);
request->content = g_strdup (content);
g_hash_table_replace (mock_httpd->requests, request->path, request);
}
void
govirt_mock_httpd_start (GovirtMockHttpd *mock_httpd)
{
g_return_if_fail(mock_httpd->thread == NULL);
mock_httpd->thread = g_thread_new ("simple-soup-httpd",
govirt_httpd_run, mock_httpd);
g_mutex_lock(&run_mutex);
}
static gboolean
govirt_mock_httpd_stop_idle (gpointer user_data)
{
GMainLoop *loop = user_data;
if (g_main_loop_is_running (loop)) {
g_main_loop_quit (loop);
}
return G_SOURCE_REMOVE;
}
void
govirt_mock_httpd_stop (GovirtMockHttpd *mock_httpd)
{
GMainContext *context;
GSource *idle_source;
context = g_main_loop_get_context (mock_httpd->loop);
idle_source = g_idle_source_new ();
g_source_set_callback (idle_source,
govirt_mock_httpd_stop_idle,
g_main_loop_ref (mock_httpd->loop),
(GDestroyNotify)g_main_loop_unref);
g_source_attach (idle_source, context);
g_source_unref (idle_source);
g_thread_join (mock_httpd->thread);
g_hash_table_unref (mock_httpd->requests);
g_main_loop_unref (mock_httpd->loop);
g_free (mock_httpd);
g_mutex_unlock(&run_mutex);
}
|