cal: remove http_conn from EWS
[siplcs.git] / src / core / sipe-http-request.c
blobccde59dbfd9998118e310376313eb9c78f4851ec
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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <string.h>
30 #include <glib.h>
32 #include "sipmsg.h"
33 #include "sipe-backend.h"
34 #include "sipe-core.h"
35 #include "sipe-core-private.h"
36 #include "sipe-http.h"
38 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
39 #include "sipe-http-request.h"
40 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
41 #include "sipe-http-transport.h"
43 struct sipe_http_connection {
44 struct sipe_http_connection_public public;
45 GSList *pending_requests;
48 struct sipe_http_session {
49 gchar *cookie; /* extremely simplistic cookie jar :-) */
52 struct sipe_http_request {
53 struct sipe_http_connection *connection;
55 struct sipe_http_session *session;
57 gchar *path;
58 gchar *headers;
59 gchar *body; /* NULL for GET */
60 gchar *content_type; /* NULL if body == NULL */
62 const gchar *domain; /* not copied */
63 const gchar *user; /* not copied */
64 const gchar *password; /* not copied */
66 sipe_http_response_callback *cb;
67 gpointer cb_data;
69 guint32 flags;
72 #define SIPE_HTTP_REQUEST_FLAG_FIRST 0x00000001
73 #define SIPE_HTTP_REQUEST_FLAG_REDIRECT 0x00000002
75 struct sipe_http_connection_public *sipe_http_connection_new(struct sipe_core_private *sipe_private,
76 const gchar *host,
77 guint32 port)
79 struct sipe_http_connection *conn = g_new0(struct sipe_http_connection, 1);
81 conn->public.sipe_private = sipe_private;
82 conn->public.host = g_strdup(host);
83 conn->public.port = port;
85 return((struct sipe_http_connection_public *) conn);
88 static void sipe_http_request_free(struct sipe_core_private *sipe_private,
89 struct sipe_http_request *req)
91 if (req->cb)
92 /* Callback: aborted */
93 (*req->cb)(sipe_private, 0, NULL, NULL, req->cb_data);
94 g_free(req->path);
95 g_free(req->headers);
96 g_free(req->body);
97 g_free(req->content_type);
98 g_free(req);
101 static void sipe_http_request_send(struct sipe_http_connection *conn)
103 struct sipe_http_request *req = conn->pending_requests->data;
104 gchar *header;
105 gchar *content = NULL;
106 gchar *cookie = NULL;
108 if (req->body)
109 content = g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT "\r\n"
110 "Content-Type: %s\r\n",
111 strlen(req->body),
112 req->content_type);
114 if (req->session && req->session->cookie)
115 cookie = g_strdup_printf("Cookie: %s\r\n", req->session->cookie);
117 header = g_strdup_printf("%s /%s HTTP/1.1\r\n"
118 "Host: %s\r\n"
119 "User-Agent: Sipe/" PACKAGE_VERSION "\r\n"
120 "%s%s%s",
121 content ? "POST" : "GET",
122 req->path,
123 conn->public.host,
124 req->headers ? req->headers : "",
125 cookie ? cookie : "",
126 content ? content : "");
127 g_free(cookie);
128 g_free(content);
130 sipe_http_transport_send((struct sipe_http_connection_public *) conn,
131 header,
132 req->body);
133 g_free(header);
136 gboolean sipe_http_request_pending(struct sipe_http_connection_public *conn_public)
138 return(((struct sipe_http_connection *) conn_public)->pending_requests != NULL);
141 void sipe_http_request_next(struct sipe_http_connection_public *conn_public)
143 sipe_http_request_send((struct sipe_http_connection *) conn_public);
146 void sipe_http_request_response(struct sipe_http_connection_public *conn_public,
147 struct sipmsg *msg)
149 struct sipe_core_private *sipe_private = conn_public->sipe_private;
150 struct sipe_http_connection *conn = (struct sipe_http_connection *) conn_public;
151 struct sipe_http_request *req = conn->pending_requests->data;
152 const gchar *hdr;
154 /* Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net */
155 if (req->session &&
156 ((hdr = sipmsg_find_header(msg, "Set-Cookie")) != NULL)) {
157 gchar **parts, **current;
158 const gchar *part;
159 gchar *new = NULL;
161 g_free(req->session->cookie);
162 req->session->cookie = NULL;
164 current = parts = g_strsplit(hdr, ";", 0);
165 while ((part = *current++) != NULL) {
166 /* strip these parts from cookie */
167 if (!(strstr(part, "path=") ||
168 strstr(part, "domain=") ||
169 strstr(part, "expires=") ||
170 strstr(part, "secure"))) {
171 gchar *tmp = new;
172 new = new ?
173 g_strconcat(new, ";", part, NULL) :
174 g_strdup(part);
175 g_free(tmp);
178 g_strfreev(parts);
180 if (new) {
181 req->session->cookie = new;
182 SIPE_DEBUG_INFO("sipe_http_request_response: cookie: %s", new);
186 /* Callback: success */
187 (*req->cb)(sipe_private,
188 msg->response,
189 msg->headers,
190 msg->body,
191 req->cb_data);
193 /* remove completed request */
194 sipe_http_request_cancel(req);
197 void sipe_http_request_shutdown(struct sipe_http_connection_public *conn_public)
199 struct sipe_http_connection *conn = (struct sipe_http_connection *) conn_public;
201 if (conn->pending_requests) {
202 GSList *entry = conn->pending_requests;
203 while (entry) {
204 sipe_http_request_free(conn_public->sipe_private,
205 entry->data);
206 entry = entry->next;
208 g_slist_free(conn->pending_requests);
211 g_free(conn->public.host);
212 g_free(conn);
215 struct sipe_http_request *sipe_http_request_new(struct sipe_core_private *sipe_private,
216 const gchar *host,
217 guint32 port,
218 const gchar *path,
219 const gchar *headers,
220 const gchar *body,
221 const gchar *content_type,
222 sipe_http_response_callback *callback,
223 gpointer callback_data)
225 struct sipe_http_request *req = g_new0(struct sipe_http_request, 1);
226 struct sipe_http_connection *conn;
228 req->path = g_strdup(path);
229 if (headers)
230 req->headers = g_strdup(headers);
231 if (body) {
232 req->body = g_strdup(body);
233 req->content_type = g_strdup(content_type);
236 /* default authentication */
237 req->domain = sipe_private->authdomain;
238 req->user = sipe_private->authuser;
239 req->password = sipe_private->password;
241 req->cb = callback;
242 req->cb_data = callback_data;
244 req->connection = conn = (struct sipe_http_connection *) sipe_http_transport_new(sipe_private,
245 host,
246 port);
247 if (conn->pending_requests == NULL)
248 req->flags = SIPE_HTTP_REQUEST_FLAG_FIRST;
250 conn->pending_requests = g_slist_append(conn->pending_requests, req);
252 return(req);
255 void sipe_http_request_ready(struct sipe_http_request *request)
257 struct sipe_http_connection *conn = request->connection;
259 /* pass first request on already opened connection through directly */
260 if ((request->flags & SIPE_HTTP_REQUEST_FLAG_FIRST) &&
261 conn->public.connected)
262 sipe_http_request_send(conn);
265 struct sipe_http_session *sipe_http_session_start(void)
267 return(g_new0(struct sipe_http_session, 1));
270 void sipe_http_session_close(struct sipe_http_session *session)
272 if (session) {
273 g_free(session->cookie);
274 g_free(session);
278 void sipe_http_request_cancel(struct sipe_http_request *request)
280 struct sipe_http_connection *conn = request->connection;
281 conn->pending_requests = g_slist_remove(conn->pending_requests,
282 request);
284 /* cancelled by requester, don't use callback */
285 request->cb = NULL;
287 sipe_http_request_free(conn->public.sipe_private, request);
290 void sipe_http_request_session(struct sipe_http_request *request,
291 struct sipe_http_session *session)
293 request->session = session;
296 void sipe_http_request_allow_redirect(struct sipe_http_request *request)
298 request->flags |= SIPE_HTTP_REQUEST_FLAG_REDIRECT;
301 void sipe_http_request_authentication(struct sipe_http_request *request,
302 const gchar *domain,
303 const gchar *user,
304 const gchar *password)
306 request->domain = domain;
307 request->user = user;
308 request->password = password;
312 Local Variables:
313 mode: c
314 c-file-style: "bsd"
315 indent-tabs-mode: t
316 tab-width: 8
317 End: