http: small cleanup in sipe-http-request.c
[siplcs.git] / src / core / sipe-http.c
blobb01fb3c1a65b9d89459baf9f0420352d9ce7985e
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 struct parsed_uri {
40 gchar *host;
41 gchar *path;
42 guint port;
45 static void parsed_uri_free(struct parsed_uri *parsed_uri)
47 g_free(parsed_uri->host);
48 g_free(parsed_uri->path);
49 g_free(parsed_uri);
52 static struct parsed_uri *sipe_http_parse_uri(const gchar *uri)
54 struct parsed_uri *parsed_uri = NULL;
56 // SIPE_DEBUG_INFO("sipe_http_parse_uri: '%s'", uri);
58 /* Currently only HTTPS is supported */
59 if (g_str_has_prefix(uri, "https://")) {
60 gchar **hostport_path = g_strsplit(uri + 8, "/", 2);
62 if (hostport_path && hostport_path[0] && hostport_path[1]) {
63 gchar **host_port = g_strsplit(hostport_path[0], ":", 2);
65 // SIPE_DEBUG_INFO("sipe_http_parse_uri: hostport '%s' path '%s'", hostport_path[0], hostport_path[1]);
67 /* ":port" is optional */
68 if (host_port && host_port[0]) {
70 parsed_uri = g_new0(struct parsed_uri, 1);
71 parsed_uri->host = g_strdup(host_port[0]);
72 parsed_uri->path = g_strdup(hostport_path[1]);
74 if (host_port[1])
75 parsed_uri->port = g_ascii_strtoull(host_port[1],
76 NULL,
77 10);
78 if (parsed_uri->port == 0)
79 /* default port for https */
80 parsed_uri->port = 443;
82 SIPE_DEBUG_INFO("sipe_http_parse_uri: host '%s' port %d path '%s'",
83 parsed_uri->host, parsed_uri->port, parsed_uri->path);
86 g_strfreev(host_port);
88 g_strfreev(hostport_path);
91 if (!parsed_uri)
92 SIPE_DEBUG_ERROR("sipe_http_parse_uri: FAILED '%s'", uri);
94 return(parsed_uri);
97 struct sipe_http_request *sipe_http_request_get(struct sipe_core_private *sipe_private,
98 const gchar *uri,
99 const gchar *headers,
100 sipe_http_response_callback *callback,
101 gpointer callback_data)
103 struct sipe_http_request *req;
104 struct parsed_uri *parsed_uri = sipe_http_parse_uri(uri);
105 if (!parsed_uri)
106 return(NULL);
108 req = sipe_http_request_new(sipe_private,
109 parsed_uri->host,
110 parsed_uri->port,
111 parsed_uri->path,
112 headers,
113 NULL,
114 NULL,
115 callback,
116 callback_data);
117 parsed_uri_free(parsed_uri);
119 return(req);
122 struct sipe_http_request *sipe_http_request_post(struct sipe_core_private *sipe_private,
123 const gchar *uri,
124 const gchar *headers,
125 const gchar *body,
126 const gchar *content_type,
127 sipe_http_response_callback *callback,
128 gpointer callback_data)
130 struct sipe_http_request *req;
131 struct parsed_uri *parsed_uri = sipe_http_parse_uri(uri);
132 if (!parsed_uri)
133 return(NULL);
135 req = sipe_http_request_new(sipe_private,
136 parsed_uri->host,
137 parsed_uri->port,
138 parsed_uri->path,
139 headers,
140 body,
141 content_type,
142 callback,
143 callback_data);
144 parsed_uri_free(parsed_uri);
146 return(req);
150 Local Variables:
151 mode: c
152 c-file-style: "bsd"
153 indent-tabs-mode: t
154 tab-width: 8
155 End: