diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-09-14 16:46:38 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-09-14 16:46:38 +0000 |
commit | f078bf6f9b737e52d3fb7f944be0d0b600686e64 (patch) | |
tree | f542e1eff8408c77ec13cea499314b769c8748a1 /src/transport/plugin_transport_http_client.c | |
parent | a86d442e62e83ef0a9744ea32ad8e09251096675 (diff) | |
download | gnunet-f078bf6f9b737e52d3fb7f944be0d0b600686e64.tar.gz gnunet-f078bf6f9b737e52d3fb7f944be0d0b600686e64.zip |
http plugin revisited
Diffstat (limited to 'src/transport/plugin_transport_http_client.c')
-rw-r--r-- | src/transport/plugin_transport_http_client.c | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_http_client.c b/src/transport/plugin_transport_http_client.c new file mode 100644 index 000000000..9a5d4b261 --- /dev/null +++ b/src/transport/plugin_transport_http_client.c | |||
@@ -0,0 +1,236 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/plugin_transport_http_client.c | ||
23 | * @brief http transport service plugin | ||
24 | * @author Matthias Wachs | ||
25 | */ | ||
26 | |||
27 | #include "plugin_transport_http.h" | ||
28 | |||
29 | #if VERBOSE_CLIENT | ||
30 | /** | ||
31 | * Function to log curl debug messages with GNUNET_log | ||
32 | * @param curl handle | ||
33 | * @param type curl_infotype | ||
34 | * @param data data | ||
35 | * @param size size | ||
36 | * @param cls closure | ||
37 | * @return 0 | ||
38 | */ | ||
39 | static int | ||
40 | client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls) | ||
41 | { | ||
42 | if (type == CURLINFO_TEXT) | ||
43 | { | ||
44 | char text[size + 2]; | ||
45 | |||
46 | memcpy (text, data, size); | ||
47 | if (text[size - 1] == '\n') | ||
48 | text[size] = '\0'; | ||
49 | else | ||
50 | { | ||
51 | text[size] = '\n'; | ||
52 | text[size + 1] = '\0'; | ||
53 | } | ||
54 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client: %X - %s", cls, text); | ||
55 | } | ||
56 | return 0; | ||
57 | } | ||
58 | #endif | ||
59 | |||
60 | int | ||
61 | client_disconnect (struct Session *s) | ||
62 | { | ||
63 | int res = GNUNET_OK; | ||
64 | CURLMcode mret; | ||
65 | |||
66 | #if DEBUG_HTTP | ||
67 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name, | ||
68 | "Deleting outbound session peer `%s'\n", | ||
69 | GNUNET_i2s (&s->target)); | ||
70 | #endif | ||
71 | |||
72 | mret = curl_multi_remove_handle (s->plugin->client_mh, s->client_put); | ||
73 | if (mret != CURLM_OK) | ||
74 | { | ||
75 | curl_easy_cleanup (s->client_put); | ||
76 | res = GNUNET_SYSERR; | ||
77 | GNUNET_break (0); | ||
78 | } | ||
79 | curl_easy_cleanup (s->client_put); | ||
80 | |||
81 | mret = curl_multi_remove_handle (s->plugin->client_mh, s->client_get); | ||
82 | if (mret != CURLM_OK) | ||
83 | { | ||
84 | curl_easy_cleanup (s->client_get); | ||
85 | res = GNUNET_SYSERR; | ||
86 | GNUNET_break (0); | ||
87 | } | ||
88 | curl_easy_cleanup (s->client_get); | ||
89 | |||
90 | return res; | ||
91 | } | ||
92 | |||
93 | int | ||
94 | client_send (struct Session *s, const char *msgbuf, size_t msgbuf_size) | ||
95 | { | ||
96 | return GNUNET_OK; | ||
97 | } | ||
98 | |||
99 | int | ||
100 | client_connect (struct Session *s) | ||
101 | { | ||
102 | int res = GNUNET_OK; | ||
103 | char *url; | ||
104 | CURLMcode mret; | ||
105 | |||
106 | #if DEBUG_HTTP | ||
107 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name, | ||
108 | "Initiating outbound session peer `%s'\n", | ||
109 | GNUNET_i2s (&s->target)); | ||
110 | #endif | ||
111 | |||
112 | s->inbound = GNUNET_NO; | ||
113 | |||
114 | /* create url */ | ||
115 | GNUNET_asprintf (&url, "%s://%s/", s->plugin->protocol, | ||
116 | http_plugin_address_to_string (NULL, s->addr, s->addrlen)); | ||
117 | |||
118 | #if DEBUG_HTTP | ||
119 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name, "URL `%s'\n", url); | ||
120 | #endif | ||
121 | |||
122 | /* create get connection */ | ||
123 | s->client_get = curl_easy_init (); | ||
124 | #if VERBOSE_CLIENT | ||
125 | curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L); | ||
126 | curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log); | ||
127 | curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get); | ||
128 | #endif | ||
129 | #if BUILD_HTTPS | ||
130 | curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); | ||
131 | curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0); | ||
132 | curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0); | ||
133 | #endif | ||
134 | curl_easy_setopt (s->client_get, CURLOPT_URL, url); | ||
135 | //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb); | ||
136 | //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps); | ||
137 | //curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, curl_send_cb); | ||
138 | //curl_easy_setopt (s->client_get, CURLOPT_READDATA, ps); | ||
139 | //curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, curl_receive_cb); | ||
140 | //curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, ps); | ||
141 | curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, | ||
142 | (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value); | ||
143 | //curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, ps); | ||
144 | curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT, | ||
145 | (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value); | ||
146 | curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE, | ||
147 | 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE); | ||
148 | #if CURL_TCP_NODELAY | ||
149 | curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1); | ||
150 | #endif | ||
151 | |||
152 | /* create put connection */ | ||
153 | s->client_put = curl_easy_init (); | ||
154 | #if VERBOSE_CLIENT | ||
155 | curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L); | ||
156 | curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log); | ||
157 | curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put); | ||
158 | #endif | ||
159 | #if BUILD_HTTPS | ||
160 | curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); | ||
161 | curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0); | ||
162 | curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0); | ||
163 | #endif | ||
164 | curl_easy_setopt (s->client_put, CURLOPT_URL, url); | ||
165 | curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L); | ||
166 | //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb); | ||
167 | //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps); | ||
168 | //curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, curl_send_cb); | ||
169 | //curl_easy_setopt (s->client_put, CURLOPT_READDATA, ps); | ||
170 | //curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, curl_receive_cb); | ||
171 | //curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, ps); | ||
172 | curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, | ||
173 | (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value); | ||
174 | //curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, ps); | ||
175 | curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT, | ||
176 | (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value); | ||
177 | curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE, | ||
178 | 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE); | ||
179 | #if CURL_TCP_NODELAY | ||
180 | curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1); | ||
181 | #endif | ||
182 | |||
183 | GNUNET_free (url); | ||
184 | |||
185 | mret = curl_multi_add_handle (s->plugin->client_mh, s->client_get); | ||
186 | if (mret != CURLM_OK) | ||
187 | { | ||
188 | curl_easy_cleanup (s->client_get); | ||
189 | res = GNUNET_SYSERR; | ||
190 | GNUNET_break (0); | ||
191 | } | ||
192 | |||
193 | mret = curl_multi_add_handle (s->plugin->client_mh, s->client_put); | ||
194 | if (mret != CURLM_OK) | ||
195 | { | ||
196 | curl_multi_remove_handle (s->plugin->client_mh, s->client_get); | ||
197 | curl_easy_cleanup (s->client_get); | ||
198 | curl_easy_cleanup (s->client_put); | ||
199 | res = GNUNET_SYSERR; | ||
200 | GNUNET_break (0); | ||
201 | } | ||
202 | |||
203 | /* Perform connect */ | ||
204 | |||
205 | return res; | ||
206 | } | ||
207 | |||
208 | int | ||
209 | client_start (struct Plugin *plugin) | ||
210 | { | ||
211 | int res = GNUNET_OK; | ||
212 | |||
213 | curl_global_init (CURL_GLOBAL_ALL); | ||
214 | plugin->client_mh = curl_multi_init (); | ||
215 | |||
216 | if (NULL == plugin->client_mh) | ||
217 | { | ||
218 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, | ||
219 | _ | ||
220 | ("Could not initialize curl multi handle, failed to start %s plugin!\n"), | ||
221 | plugin->name); | ||
222 | res = GNUNET_SYSERR; | ||
223 | } | ||
224 | return res; | ||
225 | } | ||
226 | |||
227 | void | ||
228 | client_stop (struct Plugin *plugin) | ||
229 | { | ||
230 | curl_multi_cleanup (plugin->client_mh); | ||
231 | curl_global_cleanup (); | ||
232 | } | ||
233 | |||
234 | |||
235 | |||
236 | /* end of plugin_transport_http_client.c */ | ||