buddy: remove http_conn
[siplcs.git] / src / core / sipe-http-request.c
blobbe2160491e9352b44bb3eb4a4dc0f5e3c5f27325
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-http.h"
36 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
37 #include "sipe-http-request.h"
38 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
39 #include "sipe-http-transport.h"
41 struct sipe_http_connection {
42 struct sipe_http_connection_public public;
43 GSList *pending_requests;
46 struct sipe_http_session {
47 gchar *cookie; /* extremely simplistic cookie jar :-) */
50 struct sipe_http_request {
51 struct sipe_http_connection *connection;
53 struct sipe_http_session *session;
55 gchar *path;
56 gchar *headers;
57 gchar *body; /* NULL for GET */
58 gchar *content_type; /* NULL if body == NULL */
60 sipe_http_response_callback *cb;
61 gpointer cb_data;
64 struct sipe_http_connection_public *sipe_http_connection_new(struct sipe_core_private *sipe_private,
65 const gchar *host,
66 guint32 port)
68 struct sipe_http_connection *conn = g_new0(struct sipe_http_connection, 1);
70 conn->public.sipe_private = sipe_private;
71 conn->public.host = g_strdup(host);
72 conn->public.port = port;
74 return((struct sipe_http_connection_public *) conn);
77 static void sipe_http_request_free(struct sipe_core_private *sipe_private,
78 struct sipe_http_request *req)
80 if (req->cb)
81 /* Callback: aborted */
82 (*req->cb)(sipe_private, 0, NULL, NULL, req->cb_data);
83 g_free(req->path);
84 g_free(req->headers);
85 g_free(req->body);
86 g_free(req->content_type);
87 g_free(req);
90 static void sipe_http_request_send(struct sipe_http_connection *conn)
92 struct sipe_http_request *req = conn->pending_requests->data;
93 gchar *header;
94 gchar *content = NULL;
95 gchar *cookie = NULL;
97 if (req->body)
98 content = g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT "\r\n"
99 "Content-Type: %s\r\n",
100 strlen(req->body),
101 req->content_type);
103 if (req->session && req->session->cookie)
104 cookie = g_strdup_printf("Cookie: %s\r\n", req->session->cookie);
106 header = g_strdup_printf("%s /%s HTTP/1.1\r\n"
107 "Host: %s\r\n"
108 "User-Agent: Sipe/" PACKAGE_VERSION "\r\n"
109 "%s%s%s",
110 content ? "POST" : "GET",
111 req->path,
112 conn->public.host,
113 req->headers ? req->headers : "",
114 cookie ? cookie : "",
115 content ? content : "");
116 g_free(cookie);
117 g_free(content);
119 sipe_http_transport_send((struct sipe_http_connection_public *) conn,
120 header,
121 req->body);
122 g_free(header);
125 gboolean sipe_http_request_pending(struct sipe_http_connection_public *conn_public)
127 return(((struct sipe_http_connection *) conn_public)->pending_requests != NULL);
130 void sipe_http_request_next(struct sipe_http_connection_public *conn_public)
132 sipe_http_request_send((struct sipe_http_connection *) conn_public);
135 void sipe_http_request_response(struct sipe_http_connection_public *conn_public,
136 struct sipmsg *msg)
138 struct sipe_core_private *sipe_private = conn_public->sipe_private;
139 struct sipe_http_connection *conn = (struct sipe_http_connection *) conn_public;
140 struct sipe_http_request *req = conn->pending_requests->data;
141 const gchar *hdr;
143 /* Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net */
144 if (req->session &&
145 ((hdr = sipmsg_find_header(msg, "Set-Cookie")) != NULL)) {
146 gchar **parts, **current;
147 const gchar *part;
148 gchar *new = NULL;
150 g_free(req->session->cookie);
151 req->session->cookie = NULL;
153 current = parts = g_strsplit(hdr, ";", 0);
154 while ((part = *current++) != NULL) {
155 /* strip these parts from cookie */
156 if (!(strstr(part, "path=") ||
157 strstr(part, "domain=") ||
158 strstr(part, "expires=") ||
159 strstr(part, "secure"))) {
160 gchar *tmp = new;
161 new = new ?
162 g_strconcat(new, ";", part, NULL) :
163 g_strdup(part);
164 g_free(tmp);
167 g_strfreev(parts);
169 if (new) {
170 req->session->cookie = new;
171 SIPE_DEBUG_INFO("sipe_http_request_response: cookie: %s", new);
175 /* Callback: success */
176 (*req->cb)(sipe_private,
177 msg->response,
178 msg->headers,
179 msg->body,
180 req->cb_data);
182 /* remove completed request */
183 sipe_http_request_cancel(req);
186 void sipe_http_request_shutdown(struct sipe_http_connection_public *conn_public)
188 struct sipe_http_connection *conn = (struct sipe_http_connection *) conn_public;
190 if (conn->pending_requests) {
191 GSList *entry = conn->pending_requests;
192 while (entry) {
193 sipe_http_request_free(conn_public->sipe_private,
194 entry->data);
195 entry = entry->next;
197 g_slist_free(conn->pending_requests);
200 g_free(conn->public.host);
201 g_free(conn);
204 struct sipe_http_request *sipe_http_request_new(struct sipe_core_private *sipe_private,
205 const gchar *host,
206 guint32 port,
207 const gchar *path,
208 const gchar *headers,
209 const gchar *body,
210 const gchar *content_type,
211 sipe_http_response_callback *callback,
212 gpointer callback_data)
214 struct sipe_http_request *req = g_new0(struct sipe_http_request, 1);
215 struct sipe_http_connection *conn;
216 gboolean initial;
218 req->path = g_strdup(path);
219 if (headers)
220 req->headers = g_strdup(headers);
221 if (body) {
222 req->body = g_strdup(body);
223 req->content_type = g_strdup(content_type);
226 req->cb = callback;
227 req->cb_data = callback_data;
229 req->connection = conn = (struct sipe_http_connection *) sipe_http_transport_new(sipe_private,
230 host,
231 port);
232 initial = conn->pending_requests == NULL;
234 conn->pending_requests = g_slist_append(conn->pending_requests, req);
236 /* pass first request on already opened connection through directly */
237 if (initial && conn->public.connected)
238 sipe_http_request_send(conn);
240 return(req);
243 struct sipe_http_session *sipe_http_session_start(void)
245 return(g_new0(struct sipe_http_session, 1));
248 void sipe_http_session_close(struct sipe_http_session *session)
250 if (session) {
251 g_free(session->cookie);
252 g_free(session);
256 void sipe_http_request_cancel(struct sipe_http_request *request)
258 struct sipe_http_connection *conn = request->connection;
259 conn->pending_requests = g_slist_remove(conn->pending_requests,
260 request);
262 /* cancelled by requester, don't use callback */
263 request->cb = NULL;
265 sipe_http_request_free(conn->public.sipe_private, request);
268 void sipe_http_request_session(struct sipe_http_request *request,
269 struct sipe_http_session *session)
271 request->session = session;
275 Local Variables:
276 mode: c
277 c-file-style: "bsd"
278 indent-tabs-mode: t
279 tab-width: 8
280 End: