diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-07-30 18:55:30 +0200 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-07-30 18:55:30 +0200 |
commit | 71022180e5cb1aa1ce4399c7b852cf248045daa2 (patch) | |
tree | 80aa02ad5c6a276ff4b637a113ba21b845c06fd5 | |
parent | 8ffc458b7acfc3179dd793b65ac6db3e5c79cd3b (diff) | |
download | merchant-71022180e5cb1aa1ce4399c7b852cf248045daa2.tar.gz merchant-71022180e5cb1aa1ce4399c7b852cf248045daa2.zip |
adding hello_world-ish merchant backend
-rw-r--r-- | src/backend/taler-merchant-httpd.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c new file mode 100644 index 00000000..152f471a --- /dev/null +++ b/src/backend/taler-merchant-httpd.c | |||
@@ -0,0 +1,175 @@ | |||
1 | /* | ||
2 | This file is part of TALER | ||
3 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | TALER is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file merchant/backend/taler-merchant-httpd.c | ||
19 | * @brief HTTP serving layer mainly intended to communicate with the frontend | ||
20 | * @author Marcello Stanisci | ||
21 | */ | ||
22 | |||
23 | #include <microhttpd.h> | ||
24 | #include <gnunet/gnunet_util_lib.h> | ||
25 | |||
26 | // task 1. Just implement a hello world server launched a` la GNUNET | ||
27 | |||
28 | /** | ||
29 | * The port we are running on | ||
30 | */ | ||
31 | static long long unsigned port; | ||
32 | |||
33 | /** | ||
34 | * The MHD Daemon | ||
35 | */ | ||
36 | static struct MHD_Daemon *mhd; | ||
37 | |||
38 | /** | ||
39 | * Generate the 'hello world' response | ||
40 | * @param connection a MHD connection | ||
41 | * @param resp where to store the response for the calling function | ||
42 | * @return HTTP status code reflecting the operation outcome | ||
43 | * | ||
44 | */ | ||
45 | |||
46 | static unsigned int | ||
47 | generate_hello (struct MHD_Connection *connection, | ||
48 | struct MHD_Response **resp) // this parameter was preceded by a '_' in its original file. Why? | ||
49 | { | ||
50 | |||
51 | const char *hello = "Hello customer"; | ||
52 | unsigned int ret; | ||
53 | |||
54 | *resp = MHD_create_response_from_buffer (strlen (hello), (void *) hello, | ||
55 | MHD_RESPMEM_PERSISTENT); | ||
56 | ret = 200; | ||
57 | return ret; | ||
58 | |||
59 | |||
60 | } | ||
61 | |||
62 | /** | ||
63 | * A client has requested the given url using the given method | ||
64 | * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT, | ||
65 | * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc). The callback | ||
66 | * must call MHD callbacks to provide content to give back to the | ||
67 | * client and return an HTTP status code (i.e. #MHD_HTTP_OK, | ||
68 | * #MHD_HTTP_NOT_FOUND, etc.). | ||
69 | * | ||
70 | * @param cls argument given together with the function | ||
71 | * pointer when the handler was registered with MHD | ||
72 | * @param url the requested url | ||
73 | * @param method the HTTP method used (#MHD_HTTP_METHOD_GET, | ||
74 | * #MHD_HTTP_METHOD_PUT, etc.) | ||
75 | * @param version the HTTP version string (i.e. | ||
76 | * #MHD_HTTP_VERSION_1_1) | ||
77 | * @param upload_data the data being uploaded (excluding HEADERS, | ||
78 | * for a POST that fits into memory and that is encoded | ||
79 | * with a supported encoding, the POST data will NOT be | ||
80 | * given in upload_data and is instead available as | ||
81 | * part of #MHD_get_connection_values; very large POST | ||
82 | * data *will* be made available incrementally in | ||
83 | * @a upload_data) | ||
84 | * @param upload_data_size set initially to the size of the | ||
85 | * @a upload_data provided; the method must update this | ||
86 | * value to the number of bytes NOT processed; | ||
87 | * @param con_cls pointer that the callback can set to some | ||
88 | * address and that will be preserved by MHD for future | ||
89 | * calls for this request; since the access handler may | ||
90 | * be called many times (i.e., for a PUT/POST operation | ||
91 | * with plenty of upload data) this allows the application | ||
92 | * to easily associate some request-specific state. | ||
93 | * If necessary, this state can be cleaned up in the | ||
94 | * global #MHD_RequestCompletedCallback (which | ||
95 | * can be set with the #MHD_OPTION_NOTIFY_COMPLETED). | ||
96 | * Initially, `*con_cls` will be NULL. | ||
97 | * @return #MHD_YES if the connection was handled successfully, | ||
98 | * #MHD_NO if the socket must be closed due to a serios | ||
99 | * error while handling the request | ||
100 | */ | ||
101 | |||
102 | static int | ||
103 | url_handler (void *cls, | ||
104 | struct MHD_Connection *connection, | ||
105 | const char *url, | ||
106 | const char *method, | ||
107 | const char *version, | ||
108 | const char *upload_data, | ||
109 | size_t *upload_data_size, | ||
110 | void **con_cls) | ||
111 | { | ||
112 | |||
113 | unsigned int status; | ||
114 | struct MHD_Response *resp; | ||
115 | |||
116 | #define URL_HELLO "/hello" | ||
117 | #define STR_404_NOTFOUND "The requested resource is not found" | ||
118 | |||
119 | if (0 == strncasecmp (url, URL_HELLO, sizeof (URL_HELLO))) | ||
120 | { | ||
121 | /* parse for /contract */ | ||
122 | if (0 == strcmp (MHD_HTTP_METHOD_GET, method)) | ||
123 | status = generate_hello (connection, &resp); //TBD | ||
124 | else | ||
125 | GNUNET_break (0); /* FIXME: implement for post */ | ||
126 | } | ||
127 | |||
128 | |||
129 | if (NULL != resp) | ||
130 | { | ||
131 | EXITIF (MHD_YES != MHD_queue_response (connection, status, resp)); | ||
132 | if (!no_destroy) | ||
133 | MHD_destroy_response (resp); | ||
134 | } | ||
135 | else | ||
136 | EXITIF (GNUNET_OK != failure_resp (connection, status)); | ||
137 | return MHD_YES; | ||
138 | |||
139 | EXITIF_exit: | ||
140 | result = GNUNET_SYSERR; | ||
141 | //GNUNET_SCHEDULER_shutdown (); to a later stage, maybe | ||
142 | return MHD_NO; | ||
143 | |||
144 | } | ||
145 | |||
146 | |||
147 | |||
148 | /** | ||
149 | * The main function of the serve tool | ||
150 | * | ||
151 | * @param argc number of arguments from the command line | ||
152 | * @param argv command line arguments | ||
153 | * @return 0 ok, 1 on error | ||
154 | */ | ||
155 | int | ||
156 | main (int argc, char *const *argv) | ||
157 | { | ||
158 | |||
159 | mhd = MHD_start_daemon (MHD_USE_DEBUG, //| MHD_USE_TCP_FASTOPEN, | ||
160 | (unsigned short) port, | ||
161 | NULL, NULL, | ||
162 | &url_handler, NULL, | ||
163 | //MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE, | ||
164 | //(unsigned int) 16, | ||
165 | MHD_OPTION_END); | ||
166 | |||
167 | getchar (); | ||
168 | MHD_stop_daemon (daemon); | ||
169 | return 0; | ||
170 | |||
171 | |||
172 | |||
173 | |||
174 | |||
175 | } | ||