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
|
/* Tidbit
* Copyright (C) 2010 Charlie Brej tidbit@brej.org
*
* 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 3.0 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <glib.h>
#include <libsoup/soup.h>
#include <string.h>
#include "tidbit-http.h"
#include "tidbit-database.h"
#include "tidbit-record.h"
#define INDEX_HTML "<html></html>"
#define MAIN_HTML "<html><head><script language=\"javascript\" src=\"/javascript.js\"></script></head><body><h1>hah</h1></html>"
#define JAVASCRIPT "function blah () { var a = 1; }"
static void tidbit_http_callback_insert (SoupServer *server, SoupMessage *msg, const char *path,
GHashTable *http_query, SoupClientContext *context, gpointer user_data)
{
PtrTidbitDatabase database = (PtrTidbitDatabase) user_data;
PtrTidbitRecord record = tidbit_record_from_raw ((char *) msg->request_body->data); //FIXME check record is ok
g_assert(record);
g_assert(record->status == TIDBIT_RECORD_STATUS_SIGNED);
// g_print("inserted %s\n", record->guid->string);
tidbit_database_insert_record (database, record);
soup_message_set_status (msg, SOUP_STATUS_OK);
soup_message_set_response (msg, "text/plain", SOUP_MEMORY_STATIC, "KTHXBAI", strlen("KTHXBAI"));
}
static void tidbit_http_callback_fetch (SoupServer *server, SoupMessage *msg, const char *path,
GHashTable *http_query, SoupClientContext *context, gpointer user_data)
{
PtrTidbitDatabase database = (PtrTidbitDatabase) user_data;
char* guid_str = g_hash_table_lookup(http_query, "guid");
// g_print("fetch: %s\n", guid_str);
g_assert(guid_str);
PtrTidbitGuid guid = tidbit_guid_new (guid_str);
PtrTidbitRecord record = tidbit_database_fetch_record (database, guid);
g_assert(record);
soup_message_set_status (msg, SOUP_STATUS_OK);
soup_message_set_response (msg, "text/plain", SOUP_MEMORY_COPY, record->raw, strlen(record->raw));
}
static void guid_set_to_string_gstring_func (PtrTidbitGuid guid, gpointer user_data)
{
GString* string = (GString*) user_data;
g_string_append (string, tidbit_guid_get_string (guid));
g_string_append_c (string, '\n');
}
static void tidbit_http_callback_query (SoupServer *server, SoupMessage *msg, const char *path,
GHashTable *http_query, SoupClientContext *context, gpointer user_data)
{
PtrTidbitDatabase database = (PtrTidbitDatabase) user_data;
GList* keys = g_hash_table_get_keys(http_query);
char* table_name = g_hash_table_lookup(http_query, "tidbit_table");
PtrTidbitQuery query = tidbit_query_new (table_name);
for (GList* keys_itter = keys; keys_itter; keys_itter = g_list_next(keys_itter)){
char* key = keys_itter->data;
if (strcmp(key, "tidbit_table")){
tidbit_query_add_exp (query, key, g_hash_table_lookup(http_query, key), TIDBIT_QUERY_EXP_TYPE_EQ);
}
}
g_list_free(keys);
PtrTidbitGuidSet guid_set = tidbit_database_query (database, query);
GString* reply_str = g_string_sized_new (tidbit_guid_set_get_count (guid_set) * 174);
tidbit_guid_set_foreach (guid_set, guid_set_to_string_gstring_func, reply_str);
tidbit_guid_set_unref(guid_set);
soup_message_set_status (msg, SOUP_STATUS_OK);
soup_message_set_response (msg, "text/plain", SOUP_MEMORY_COPY, reply_str->str, reply_str->len);
g_string_free (reply_str, TRUE);
}
void tidbit_http_setup (PtrTidbitDatabase database)
{
SoupServer* server = soup_server_new (SOUP_SERVER_PORT, 7180, NULL);
soup_server_run_async (server);
soup_server_add_handler (server, "/insert", tidbit_http_callback_insert, database, NULL);
soup_server_add_handler (server, "/fetch", tidbit_http_callback_fetch, database, NULL);
soup_server_add_handler (server, "/query", tidbit_http_callback_query, database, NULL);
}
|