2 * @file sipe-http-request.c
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
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
;
49 gchar
*body
; /* NULL for GET */
50 gchar
*content_type
; /* NULL if body == NULL */
52 sipe_http_response_callback
*cb
;
56 struct sipe_http_connection_public
*sipe_http_connection_new(struct sipe_core_private
*sipe_private
,
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
)
73 /* Callback: aborted */
74 (*req
->cb
)(sipe_private
, 0, NULL
, req
->cb_data
);
78 g_free(req
->content_type
);
82 static void sipe_http_request_send(struct sipe_http_connection
*conn
)
84 struct sipe_http_request
*req
= conn
->pending_requests
->data
;
86 gchar
*content
= NULL
;
89 content
= g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT
"\r\n"
90 "Content-Type: %s\r\n",
94 header
= g_strdup_printf("%s /%s HTTP/1.1\r\n"
96 "User-Agent: Sipe/" PACKAGE_VERSION
"\r\n"
98 content
? "POST" : "GET",
101 req
->headers
? req
->headers
: "",
102 content
? content
: "");
105 sipe_http_transport_send((struct sipe_http_connection_public
*) conn
,
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
,
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
;
143 sipe_http_request_free(conn_public
->sipe_private
,
147 g_slist_free(conn
->pending_requests
);
150 g_free(conn
->public.host
);
154 struct sipe_http_request
*sipe_http_request_new(struct sipe_core_private
*sipe_private
,
158 const gchar
*headers
,
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
;
168 req
->path
= g_strdup(path
);
170 req
->headers
= g_strdup(headers
);
172 req
->body
= g_strdup(body
);
173 req
->content_type
= g_strdup(content_type
);
177 req
->cb_data
= callback_data
;
179 req
->connection
= conn
= (struct sipe_http_connection
*) sipe_http_transport_new(sipe_private
,
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
);
193 struct sipe_http_session
*sipe_http_session_start(void)
198 void sipe_http_session_close(struct sipe_http_session
*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
,
209 /* cancelled by requester, don't use callback */
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
)