http: refactor connection data structure handling
[siplcs.git] / src / core / sipe-http-request.c
blob288b6eedb152a1a91d13e3d26e826b6bea8e9bad
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 struct sipe_http_session {
53 gchar *cookie; /* extremely simplistic cookie jar :-) */
56 struct sipe_http_request {
57 struct sipe_http_connection_public *connection;
59 struct sipe_http_session *session;
61 gchar *path;
62 gchar *headers;
63 gchar *body; /* NULL for GET */
64 gchar *content_type; /* NULL if body == NULL */
66 const gchar *domain; /* not copied */
67 const gchar *user; /* not copied */
68 const gchar *password; /* not copied */
70 sipe_http_response_callback *cb;
71 gpointer cb_data;
73 guint32 flags;
76 #define SIPE_HTTP_REQUEST_FLAG_FIRST 0x00000001
77 #define SIPE_HTTP_REQUEST_FLAG_REDIRECT 0x00000002
79 static void sipe_http_request_free(struct sipe_core_private *sipe_private,
80 struct sipe_http_request *req)
82 if (req->cb)
83 /* Callback: aborted */
84 (*req->cb)(sipe_private, 0, NULL, NULL, req->cb_data);
85 g_free(req->path);
86 g_free(req->headers);
87 g_free(req->body);
88 g_free(req->content_type);
89 g_free(req);
92 static void sipe_http_request_send(struct sipe_http_connection_public *conn_public)
94 struct sipe_http_request *req = conn_public->pending_requests->data;
95 gchar *header;
96 gchar *content = NULL;
97 gchar *cookie = NULL;
99 if (req->body)
100 content = g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT "\r\n"
101 "Content-Type: %s\r\n",
102 strlen(req->body),
103 req->content_type);
105 if (req->session && req->session->cookie)
106 cookie = g_strdup_printf("Cookie: %s\r\n", req->session->cookie);
108 header = g_strdup_printf("%s /%s HTTP/1.1\r\n"
109 "Host: %s\r\n"
110 "User-Agent: Sipe/" PACKAGE_VERSION "\r\n"
111 "%s%s%s",
112 content ? "POST" : "GET",
113 req->path,
114 conn_public->host,
115 req->headers ? req->headers : "",
116 cookie ? cookie : "",
117 content ? content : "");
118 g_free(cookie);
119 g_free(content);
121 sipe_http_transport_send(conn_public,
122 header,
123 req->body);
124 g_free(header);
127 gboolean sipe_http_request_pending(struct sipe_http_connection_public *conn_public)
129 return(conn_public->pending_requests != NULL);
132 void sipe_http_request_next(struct sipe_http_connection_public *conn_public)
134 sipe_http_request_send(conn_public);
137 void sipe_http_request_response(struct sipe_http_connection_public *conn_public,
138 struct sipmsg *msg)
140 struct sipe_core_private *sipe_private = conn_public->sipe_private;
141 struct sipe_http_request *req = conn_public->pending_requests->data;
142 const gchar *hdr;
144 /* Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net */
145 if (req->session &&
146 ((hdr = sipmsg_find_header(msg, "Set-Cookie")) != NULL)) {
147 gchar **parts, **current;
148 const gchar *part;
149 gchar *new = NULL;
151 g_free(req->session->cookie);
152 req->session->cookie = NULL;
154 current = parts = g_strsplit(hdr, ";", 0);
155 while ((part = *current++) != NULL) {
156 /* strip these parts from cookie */
157 if (!(strstr(part, "path=") ||
158 strstr(part, "domain=") ||
159 strstr(part, "expires=") ||
160 strstr(part, "secure"))) {
161 gchar *tmp = new;
162 new = new ?
163 g_strconcat(new, ";", part, NULL) :
164 g_strdup(part);
165 g_free(tmp);
168 g_strfreev(parts);
170 if (new) {
171 req->session->cookie = new;
172 SIPE_DEBUG_INFO("sipe_http_request_response: cookie: %s", new);
176 /* Callback: success */
177 (*req->cb)(sipe_private,
178 msg->response,
179 msg->headers,
180 msg->body,
181 req->cb_data);
183 /* remove completed request */
184 sipe_http_request_cancel(req);
187 void sipe_http_request_shutdown(struct sipe_http_connection_public *conn_public)
189 if (conn_public->pending_requests) {
190 GSList *entry = conn_public->pending_requests;
191 while (entry) {
192 sipe_http_request_free(conn_public->sipe_private,
193 entry->data);
194 entry = entry->next;
196 g_slist_free(conn_public->pending_requests);
197 conn_public->pending_requests = NULL;
201 struct sipe_http_request *sipe_http_request_new(struct sipe_core_private *sipe_private,
202 const gchar *host,
203 guint32 port,
204 const gchar *path,
205 const gchar *headers,
206 const gchar *body,
207 const gchar *content_type,
208 sipe_http_response_callback *callback,
209 gpointer callback_data)
211 struct sipe_http_request *req = g_new0(struct sipe_http_request, 1);
212 struct sipe_http_connection_public *conn_public;
214 req->path = g_strdup(path);
215 if (headers)
216 req->headers = g_strdup(headers);
217 if (body) {
218 req->body = g_strdup(body);
219 req->content_type = g_strdup(content_type);
222 /* default authentication */
223 req->domain = sipe_private->authdomain;
224 req->user = sipe_private->authuser;
225 req->password = sipe_private->password;
227 req->cb = callback;
228 req->cb_data = callback_data;
230 req->connection = conn_public = sipe_http_transport_new(sipe_private,
231 host,
232 port);
233 if (!sipe_http_request_pending(conn_public))
234 req->flags = SIPE_HTTP_REQUEST_FLAG_FIRST;
236 conn_public->pending_requests = g_slist_append(conn_public->pending_requests,
237 req);
239 return(req);
242 void sipe_http_request_ready(struct sipe_http_request *request)
244 struct sipe_http_connection_public *conn_public = request->connection;
246 /* pass first request on already opened connection through directly */
247 if ((request->flags & SIPE_HTTP_REQUEST_FLAG_FIRST) &&
248 conn_public->connected)
249 sipe_http_request_send(conn_public);
252 struct sipe_http_session *sipe_http_session_start(void)
254 return(g_new0(struct sipe_http_session, 1));
257 void sipe_http_session_close(struct sipe_http_session *session)
259 if (session) {
260 g_free(session->cookie);
261 g_free(session);
265 void sipe_http_request_cancel(struct sipe_http_request *request)
267 struct sipe_http_connection_public *conn_public = request->connection;
268 conn_public->pending_requests = g_slist_remove(conn_public->pending_requests,
269 request);
271 /* cancelled by requester, don't use callback */
272 request->cb = NULL;
274 sipe_http_request_free(conn_public->sipe_private, request);
277 void sipe_http_request_session(struct sipe_http_request *request,
278 struct sipe_http_session *session)
280 request->session = session;
283 void sipe_http_request_allow_redirect(struct sipe_http_request *request)
285 request->flags |= SIPE_HTTP_REQUEST_FLAG_REDIRECT;
288 void sipe_http_request_authentication(struct sipe_http_request *request,
289 const gchar *domain,
290 const gchar *user,
291 const gchar *password)
293 request->domain = domain;
294 request->user = user;
295 request->password = password;
299 Local Variables:
300 mode: c
301 c-file-style: "bsd"
302 indent-tabs-mode: t
303 tab-width: 8
304 End: