http: small cleanup in sipe-http-request.c
[siplcs.git] / src / core / sipe-http-request.c
blob75b03e838633a79e0fa0ec6a982dc9fe0402db68
1 /**
2 * @file sipe-http-request.c
4 * pidgin-sipe
6 * Copyright (C) 2013 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * SIPE HTTP request layer implementation
26 * - request handling: creation, parameters, deletion, cancelling
27 * - session handling: creation, closing
28 * - connection request queue handling
29 * - compile HTTP header contents and hand-off to transport layer
30 * - process HTTP response and hand-off to user callback
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
37 #include <string.h>
39 #include <glib.h>
41 #include "sipmsg.h"
42 #include "sipe-backend.h"
43 #include "sipe-core.h"
44 #include "sipe-core-private.h"
45 #include "sipe-http.h"
47 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
48 #include "sipe-http-request.h"
49 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
50 #include "sipe-http-transport.h"
52 #define SIPE_HTTP_CONNECTION ((struct sipe_http_connection *) conn_public)
53 #define SIPE_HTTP_CONNECTION_PUBLIC ((struct sipe_http_connection_public *) conn)
55 struct sipe_http_connection {
56 struct sipe_http_connection_public public;
57 GSList *pending_requests;
60 struct sipe_http_session {
61 gchar *cookie; /* extremely simplistic cookie jar :-) */
64 struct sipe_http_request {
65 struct sipe_http_connection *connection;
67 struct sipe_http_session *session;
69 gchar *path;
70 gchar *headers;
71 gchar *body; /* NULL for GET */
72 gchar *content_type; /* NULL if body == NULL */
74 const gchar *domain; /* not copied */
75 const gchar *user; /* not copied */
76 const gchar *password; /* not copied */
78 sipe_http_response_callback *cb;
79 gpointer cb_data;
81 guint32 flags;
84 #define SIPE_HTTP_REQUEST_FLAG_FIRST 0x00000001
85 #define SIPE_HTTP_REQUEST_FLAG_REDIRECT 0x00000002
87 struct sipe_http_connection_public *sipe_http_connection_new(struct sipe_core_private *sipe_private,
88 const gchar *host,
89 guint32 port)
91 struct sipe_http_connection *conn = g_new0(struct sipe_http_connection, 1);
93 conn->public.sipe_private = sipe_private;
94 conn->public.host = g_strdup(host);
95 conn->public.port = port;
97 return(SIPE_HTTP_CONNECTION_PUBLIC);
100 static void sipe_http_request_free(struct sipe_core_private *sipe_private,
101 struct sipe_http_request *req)
103 if (req->cb)
104 /* Callback: aborted */
105 (*req->cb)(sipe_private, 0, NULL, NULL, req->cb_data);
106 g_free(req->path);
107 g_free(req->headers);
108 g_free(req->body);
109 g_free(req->content_type);
110 g_free(req);
113 static void sipe_http_request_send(struct sipe_http_connection *conn)
115 struct sipe_http_request *req = conn->pending_requests->data;
116 gchar *header;
117 gchar *content = NULL;
118 gchar *cookie = NULL;
120 if (req->body)
121 content = g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT "\r\n"
122 "Content-Type: %s\r\n",
123 strlen(req->body),
124 req->content_type);
126 if (req->session && req->session->cookie)
127 cookie = g_strdup_printf("Cookie: %s\r\n", req->session->cookie);
129 header = g_strdup_printf("%s /%s HTTP/1.1\r\n"
130 "Host: %s\r\n"
131 "User-Agent: Sipe/" PACKAGE_VERSION "\r\n"
132 "%s%s%s",
133 content ? "POST" : "GET",
134 req->path,
135 conn->public.host,
136 req->headers ? req->headers : "",
137 cookie ? cookie : "",
138 content ? content : "");
139 g_free(cookie);
140 g_free(content);
142 sipe_http_transport_send(SIPE_HTTP_CONNECTION_PUBLIC,
143 header,
144 req->body);
145 g_free(header);
148 gboolean sipe_http_request_pending(struct sipe_http_connection_public *conn_public)
150 return(SIPE_HTTP_CONNECTION->pending_requests != NULL);
153 void sipe_http_request_next(struct sipe_http_connection_public *conn_public)
155 sipe_http_request_send(SIPE_HTTP_CONNECTION);
158 void sipe_http_request_response(struct sipe_http_connection_public *conn_public,
159 struct sipmsg *msg)
161 struct sipe_core_private *sipe_private = conn_public->sipe_private;
162 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
163 struct sipe_http_request *req = conn->pending_requests->data;
164 const gchar *hdr;
166 /* Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net */
167 if (req->session &&
168 ((hdr = sipmsg_find_header(msg, "Set-Cookie")) != NULL)) {
169 gchar **parts, **current;
170 const gchar *part;
171 gchar *new = NULL;
173 g_free(req->session->cookie);
174 req->session->cookie = NULL;
176 current = parts = g_strsplit(hdr, ";", 0);
177 while ((part = *current++) != NULL) {
178 /* strip these parts from cookie */
179 if (!(strstr(part, "path=") ||
180 strstr(part, "domain=") ||
181 strstr(part, "expires=") ||
182 strstr(part, "secure"))) {
183 gchar *tmp = new;
184 new = new ?
185 g_strconcat(new, ";", part, NULL) :
186 g_strdup(part);
187 g_free(tmp);
190 g_strfreev(parts);
192 if (new) {
193 req->session->cookie = new;
194 SIPE_DEBUG_INFO("sipe_http_request_response: cookie: %s", new);
198 /* Callback: success */
199 (*req->cb)(sipe_private,
200 msg->response,
201 msg->headers,
202 msg->body,
203 req->cb_data);
205 /* remove completed request */
206 sipe_http_request_cancel(req);
209 void sipe_http_request_shutdown(struct sipe_http_connection_public *conn_public)
211 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
213 if (conn->pending_requests) {
214 GSList *entry = conn->pending_requests;
215 while (entry) {
216 sipe_http_request_free(conn_public->sipe_private,
217 entry->data);
218 entry = entry->next;
220 g_slist_free(conn->pending_requests);
223 g_free(conn->public.host);
224 g_free(conn);
227 struct sipe_http_request *sipe_http_request_new(struct sipe_core_private *sipe_private,
228 const gchar *host,
229 guint32 port,
230 const gchar *path,
231 const gchar *headers,
232 const gchar *body,
233 const gchar *content_type,
234 sipe_http_response_callback *callback,
235 gpointer callback_data)
237 struct sipe_http_request *req = g_new0(struct sipe_http_request, 1);
238 struct sipe_http_connection *conn;
240 req->path = g_strdup(path);
241 if (headers)
242 req->headers = g_strdup(headers);
243 if (body) {
244 req->body = g_strdup(body);
245 req->content_type = g_strdup(content_type);
248 /* default authentication */
249 req->domain = sipe_private->authdomain;
250 req->user = sipe_private->authuser;
251 req->password = sipe_private->password;
253 req->cb = callback;
254 req->cb_data = callback_data;
256 req->connection = conn = (struct sipe_http_connection *) sipe_http_transport_new(sipe_private,
257 host,
258 port);
259 if (!sipe_http_request_pending(SIPE_HTTP_CONNECTION_PUBLIC))
260 req->flags = SIPE_HTTP_REQUEST_FLAG_FIRST;
262 conn->pending_requests = g_slist_append(conn->pending_requests, req);
264 return(req);
267 void sipe_http_request_ready(struct sipe_http_request *request)
269 struct sipe_http_connection *conn = request->connection;
271 /* pass first request on already opened connection through directly */
272 if ((request->flags & SIPE_HTTP_REQUEST_FLAG_FIRST) &&
273 conn->public.connected)
274 sipe_http_request_send(conn);
277 struct sipe_http_session *sipe_http_session_start(void)
279 return(g_new0(struct sipe_http_session, 1));
282 void sipe_http_session_close(struct sipe_http_session *session)
284 if (session) {
285 g_free(session->cookie);
286 g_free(session);
290 void sipe_http_request_cancel(struct sipe_http_request *request)
292 struct sipe_http_connection *conn = request->connection;
293 conn->pending_requests = g_slist_remove(conn->pending_requests,
294 request);
296 /* cancelled by requester, don't use callback */
297 request->cb = NULL;
299 sipe_http_request_free(conn->public.sipe_private, request);
302 void sipe_http_request_session(struct sipe_http_request *request,
303 struct sipe_http_session *session)
305 request->session = session;
308 void sipe_http_request_allow_redirect(struct sipe_http_request *request)
310 request->flags |= SIPE_HTTP_REQUEST_FLAG_REDIRECT;
313 void sipe_http_request_authentication(struct sipe_http_request *request,
314 const gchar *domain,
315 const gchar *user,
316 const gchar *password)
318 request->domain = domain;
319 request->user = user;
320 request->password = password;
324 Local Variables:
325 mode: c
326 c-file-style: "bsd"
327 indent-tabs-mode: t
328 tab-width: 8
329 End: