Release 1.25.0 -- Buddy Idle Time, RTF
[siplcs.git] / src / core / sipe-http.c
blobcf6975440d7787e52745734bacd903dfbe07a545
1 /**
2 * @file sipe-http.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 API implementation
26 * - convenience functions for public API: GET & POST requests
27 * - URL parsing
28 * - all other public API functions are implemented by lower layers
31 #include <glib.h>
33 #include "sipe-backend.h"
34 #include "sipe-http.h"
36 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
37 #include "sipe-http-request.h"
39 void sipe_http_parsed_uri_free(struct sipe_http_parsed_uri *parsed_uri)
41 if (parsed_uri) {
42 g_free(parsed_uri->host);
43 g_free(parsed_uri->path);
44 g_free(parsed_uri);
48 struct sipe_http_parsed_uri *sipe_http_parse_uri(const gchar *uri)
50 struct sipe_http_parsed_uri *parsed_uri = NULL;
51 guint offset = 0;
52 gboolean tls = FALSE;
54 // SIPE_DEBUG_INFO("sipe_http_parse_uri: '%s'", uri);
56 if (g_str_has_prefix(uri, "https://")) {
57 offset = 8;
58 tls = TRUE;
59 } else if (g_str_has_prefix(uri, "http://")) {
60 offset = 7;
63 if (offset) {
64 gchar **hostport_path = g_strsplit(uri + offset, "/", 2);
66 if (hostport_path && hostport_path[0] && hostport_path[1]) {
67 gchar **host_port = g_strsplit(hostport_path[0], ":", 2);
69 // SIPE_DEBUG_INFO("sipe_http_parse_uri: hostport '%s' path '%s'", hostport_path[0], hostport_path[1]);
71 /* ":port" is optional */
72 if (host_port && host_port[0]) {
74 parsed_uri = g_new0(struct sipe_http_parsed_uri, 1);
75 parsed_uri->host = g_strdup(host_port[0]);
76 parsed_uri->path = g_strdup(hostport_path[1]);
77 parsed_uri->tls = tls;
79 if (host_port[1])
80 parsed_uri->port = g_ascii_strtoull(host_port[1],
81 NULL,
82 10);
83 if (parsed_uri->port == 0) {
84 if (tls)
85 /* default port for https */
86 parsed_uri->port = 443;
87 else
88 /* default port for http */
89 parsed_uri->port = 80;
92 SIPE_DEBUG_INFO("sipe_http_parse_uri: host '%s' port %d path '%s'",
93 parsed_uri->host, parsed_uri->port, parsed_uri->path);
96 g_strfreev(host_port);
98 g_strfreev(hostport_path);
101 if (!parsed_uri)
102 SIPE_DEBUG_ERROR("sipe_http_parse_uri: FAILED '%s'", uri);
104 return(parsed_uri);
107 struct sipe_http_request *sipe_http_request_get(struct sipe_core_private *sipe_private,
108 const gchar *uri,
109 const gchar *headers,
110 sipe_http_response_callback *callback,
111 gpointer callback_data)
113 struct sipe_http_request *req;
114 struct sipe_http_parsed_uri *parsed_uri = sipe_http_parse_uri(uri);
116 req = sipe_http_request_new(sipe_private,
117 parsed_uri,
118 headers,
119 NULL,
120 NULL,
121 callback,
122 callback_data);
123 sipe_http_parsed_uri_free(parsed_uri);
125 return(req);
128 struct sipe_http_request *sipe_http_request_post(struct sipe_core_private *sipe_private,
129 const gchar *uri,
130 const gchar *headers,
131 const gchar *body,
132 const gchar *content_type,
133 sipe_http_response_callback *callback,
134 gpointer callback_data)
136 struct sipe_http_request *req;
137 struct sipe_http_parsed_uri *parsed_uri = sipe_http_parse_uri(uri);
139 req = sipe_http_request_new(sipe_private,
140 parsed_uri,
141 headers,
142 body,
143 content_type,
144 callback,
145 callback_data);
146 sipe_http_parsed_uri_free(parsed_uri);
148 return(req);
152 Local Variables:
153 mode: c
154 c-file-style: "bsd"
155 indent-tabs-mode: t
156 tab-width: 8
157 End: