http: add API for HTTP session
[siplcs.git] / src / core / sipe-http-request.c
blob75836e505f71bc19378a3be876cb06db22b9af8d
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 "sipe-http.h"
34 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
35 #include "sipe-http-request.h"
36 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
37 #include "sipe-http-transport.h"
39 struct sipe_http_connection {
40 struct sipe_http_connection_public public;
41 GSList *pending_requests;
44 struct sipe_http_request {
45 struct sipe_http_connection *connection;
47 gchar *path;
48 gchar *headers;
49 gchar *body; /* NULL for GET */
50 gchar *content_type; /* NULL if body == NULL */
52 sipe_http_response_callback *cb;
53 gpointer cb_data;
56 struct sipe_http_connection_public *sipe_http_connection_new(struct sipe_core_private *sipe_private,
57 const gchar *host,
58 guint32 port)
60 struct sipe_http_connection *conn = g_new0(struct sipe_http_connection, 1);
62 conn->public.sipe_private = sipe_private;
63 conn->public.host = g_strdup(host);
64 conn->public.port = port;
66 return((struct sipe_http_connection_public *) conn);
69 static void sipe_http_request_free(struct sipe_core_private *sipe_private,
70 struct sipe_http_request *req)
72 if (req->cb)
73 /* Callback: aborted */
74 (*req->cb)(sipe_private, 0, NULL, req->cb_data);
75 g_free(req->path);
76 g_free(req->headers);
77 g_free(req->body);
78 g_free(req->content_type);
79 g_free(req);
82 static void sipe_http_request_send(struct sipe_http_connection *conn)
84 struct sipe_http_request *req = conn->pending_requests->data;
85 gchar *header;
86 gchar *content = NULL;
88 if (req->body)
89 content = g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT "\r\n"
90 "Content-Type: %s\r\n",
91 strlen(req->body),
92 req->content_type);
94 header = g_strdup_printf("%s /%s HTTP/1.1\r\n"
95 "Host: %s\r\n"
96 "User-Agent: Sipe/" PACKAGE_VERSION "\r\n"
97 "%s%s",
98 content ? "POST" : "GET",
99 req->path,
100 conn->public.host,
101 req->headers ? req->headers : "",
102 content ? content : "");
103 g_free(content);
105 sipe_http_transport_send((struct sipe_http_connection_public *) conn,
106 header,
107 req->body);
108 g_free(header);
111 gboolean sipe_http_request_pending(struct sipe_http_connection_public *conn_public)
113 return(((struct sipe_http_connection *) conn_public)->pending_requests != NULL);
116 void sipe_http_request_next(struct sipe_http_connection_public *conn_public)
118 sipe_http_request_send((struct sipe_http_connection *) conn_public);
121 void sipe_http_request_response(struct sipe_http_connection_public *conn_public,
122 guint status,
123 const gchar *body)
125 struct sipe_core_private *sipe_private = conn_public->sipe_private;
126 struct sipe_http_connection *conn = (struct sipe_http_connection *) conn_public;
127 struct sipe_http_request *req = conn->pending_requests->data;
129 /* Callback: success */
130 (*req->cb)(sipe_private, status, body, req->cb_data);
132 /* remove completed request */
133 sipe_http_request_cancel(req);
136 void sipe_http_request_shutdown(struct sipe_http_connection_public *conn_public)
138 struct sipe_http_connection *conn = (struct sipe_http_connection *) conn_public;
140 if (conn->pending_requests) {
141 GSList *entry = conn->pending_requests;
142 while (entry) {
143 sipe_http_request_free(conn_public->sipe_private,
144 entry->data);
145 entry = entry->next;
147 g_slist_free(conn->pending_requests);
150 g_free(conn->public.host);
151 g_free(conn);
154 struct sipe_http_request *sipe_http_request_new(struct sipe_core_private *sipe_private,
155 const gchar *host,
156 guint32 port,
157 const gchar *path,
158 const gchar *headers,
159 const gchar *body,
160 const gchar *content_type,
161 sipe_http_response_callback *callback,
162 gpointer callback_data)
164 struct sipe_http_request *req = g_new0(struct sipe_http_request, 1);
165 struct sipe_http_connection *conn;
166 gboolean initial;
168 req->path = g_strdup(path);
169 if (headers)
170 req->headers = g_strdup(headers);
171 if (body) {
172 req->body = g_strdup(body);
173 req->content_type = g_strdup(content_type);
176 req->cb = callback;
177 req->cb_data = callback_data;
179 req->connection = conn = (struct sipe_http_connection *) sipe_http_transport_new(sipe_private,
180 host,
181 port);
182 initial = conn->pending_requests == NULL;
184 conn->pending_requests = g_slist_append(conn->pending_requests, req);
186 /* pass first request on already opened connection through directly */
187 if (initial && conn->public.connected)
188 sipe_http_request_send(conn);
190 return(req);
193 struct sipe_http_session *sipe_http_session_start(void)
195 return(NULL);
198 void sipe_http_session_close(struct sipe_http_session *session)
200 (void)session;
203 void sipe_http_request_cancel(struct sipe_http_request *request)
205 struct sipe_http_connection *conn = request->connection;
206 conn->pending_requests = g_slist_remove(conn->pending_requests,
207 request);
209 /* cancelled by requester, don't use callback */
210 request->cb = NULL;
212 sipe_http_request_free(conn->public.sipe_private, request);
215 void sipe_http_request_session(struct sipe_http_request *request,
216 const struct sipe_http_session *session)
218 (void)request;
219 (void)session;
223 Local Variables:
224 mode: c
225 c-file-style: "bsd"
226 indent-tabs-mode: t
227 tab-width: 8
228 End: