1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2002 Ximian, Inc. (www.ximian.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include "camel-exception.h"
34 #include "camel-http-stream.h"
35 #include "camel-mime-utils.h"
36 #include "camel-net-utils.h"
37 #include "camel-service.h" /* for hostname stuff */
38 #include "camel-session.h"
39 #include "camel-stream-buffer.h"
40 #include "camel-tcp-stream-raw.h"
43 #include "camel-tcp-stream-ssl.h"
48 static CamelStreamClass *parent_class = NULL;
50 static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n);
51 static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n);
52 static int stream_flush (CamelStream *stream);
53 static int stream_close (CamelStream *stream);
54 static int stream_reset (CamelStream *stream);
57 camel_http_stream_class_init (CamelHttpStreamClass *camel_http_stream_class)
59 CamelStreamClass *camel_stream_class =
60 CAMEL_STREAM_CLASS (camel_http_stream_class);
62 parent_class = CAMEL_STREAM_CLASS (camel_type_get_global_classfuncs (camel_stream_get_type ()));
64 /* virtual method overload */
65 camel_stream_class->read = stream_read;
66 camel_stream_class->write = stream_write;
67 camel_stream_class->flush = stream_flush;
68 camel_stream_class->close = stream_close;
69 camel_stream_class->reset = stream_reset;
73 camel_http_stream_init (gpointer object, gpointer klass)
75 CamelHttpStream *http = CAMEL_HTTP_STREAM (object);
78 http->content_type = NULL;
83 http->authrealm = NULL;
84 http->authpass = NULL;
90 camel_http_stream_finalize (CamelObject *object)
92 CamelHttpStream *http = CAMEL_HTTP_STREAM (object);
95 camel_object_unref(http->parser);
97 if (http->content_type)
98 camel_content_type_unref (http->content_type);
101 camel_header_raw_clear (&http->headers);
104 camel_object_unref(http->session);
107 camel_url_free (http->url);
110 camel_url_free (http->proxy);
112 g_free (http->authrealm);
113 g_free (http->authpass);
116 camel_object_unref(http->raw);
118 camel_object_unref(http->read);
122 camel_http_stream_get_type (void)
124 static CamelType type = CAMEL_INVALID_TYPE;
126 if (type == CAMEL_INVALID_TYPE) {
127 type = camel_type_register (camel_stream_get_type (),
129 sizeof (CamelHttpStream),
130 sizeof (CamelHttpStreamClass),
131 (CamelObjectClassInitFunc) camel_http_stream_class_init,
133 (CamelObjectInitFunc) camel_http_stream_init,
134 (CamelObjectFinalizeFunc) camel_http_stream_finalize);
141 * camel_http_stream_new:
142 * @method: HTTP method
143 * @session: active session
144 * @url: URL to act upon
146 * Return value: a http stream
149 camel_http_stream_new (CamelHttpMethod method, struct _CamelSession *session, CamelURL *url)
151 CamelHttpStream *stream;
154 g_return_val_if_fail(CAMEL_IS_SESSION(session), NULL);
155 g_return_val_if_fail(url != NULL, NULL);
157 stream = CAMEL_HTTP_STREAM (camel_object_new (camel_http_stream_get_type ()));
159 stream->method = method;
160 stream->session = session;
161 camel_object_ref(session);
163 str = camel_url_to_string (url, 0);
164 stream->url = camel_url_new (str, NULL);
167 return (CamelStream *)stream;
170 #define SSL_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_SSL2 | CAMEL_TCP_STREAM_SSL_ENABLE_SSL3)
173 http_connect (CamelHttpStream *http, CamelURL *url)
175 CamelStream *stream = NULL;
176 struct addrinfo *ai, hints = { 0 };
180 d(printf("connecting to http stream @ '%s'\n", url->host));
182 if (!g_ascii_strcasecmp (url->protocol, "https")) {
184 stream = camel_tcp_stream_ssl_new (http->session, url->host, SSL_FLAGS);
187 stream = camel_tcp_stream_raw_new ();
190 if (stream == NULL) {
197 sprintf(serv, "%d", url->port);
199 serv = url->protocol;
201 hints.ai_socktype = SOCK_STREAM;
203 ai = camel_getaddrinfo(url->host, serv, &hints, NULL);
205 camel_object_unref (stream);
209 if (camel_tcp_stream_connect (CAMEL_TCP_STREAM (stream), ai) == -1) {
211 camel_object_unref (stream);
212 camel_freeaddrinfo(ai);
217 camel_freeaddrinfo(ai);
220 http->read = camel_stream_buffer_new (stream, CAMEL_STREAM_BUFFER_READ);
226 http_disconnect(CamelHttpStream *http)
229 camel_object_unref(http->raw);
234 camel_object_unref(http->read);
239 camel_object_unref(http->parser);
245 http_next_token (const unsigned char *in)
247 const unsigned char *inptr = in;
249 while (*inptr && !isspace ((int) *inptr))
252 while (*inptr && isspace ((int) *inptr))
255 return (const char *) inptr;
259 http_get_statuscode (CamelHttpStream *http)
264 if (camel_stream_buffer_gets ((CamelStreamBuffer *)http->read, buffer, sizeof (buffer)) <= 0)
267 d(printf("HTTP Status: %s\n", buffer));
269 /* parse the HTTP status code */
270 if (!g_ascii_strncasecmp (buffer, "HTTP/", 5)) {
271 token = http_next_token (buffer);
272 http->statuscode = camel_header_decode_int (&token);
273 return http->statuscode;
276 http_disconnect(http);
282 http_get_headers (CamelHttpStream *http)
284 struct _camel_header_raw *headers, *node, *tail;
291 camel_object_unref (http->parser);
293 http->parser = camel_mime_parser_new ();
294 camel_mime_parser_init_with_stream (http->parser, http->read);
296 switch (camel_mime_parser_step (http->parser, &buf, &len)) {
297 case CAMEL_MIME_PARSER_STATE_MESSAGE:
298 case CAMEL_MIME_PARSER_STATE_HEADER:
299 headers = camel_mime_parser_headers_raw (http->parser);
300 if (http->content_type)
301 camel_content_type_unref (http->content_type);
302 type = camel_header_raw_find (&headers, "Content-Type", NULL);
304 http->content_type = camel_content_type_decode (type);
306 http->content_type = NULL;
309 camel_header_raw_clear (&http->headers);
311 http->headers = NULL;
312 tail = (struct _camel_header_raw *) &http->headers;
314 d(printf("HTTP Headers:\n"));
316 d(printf(" %s:%s\n", headers->name, headers->value));
317 node = g_new (struct _camel_header_raw, 1);
319 node->name = g_strdup (headers->name);
320 node->value = g_strdup (headers->value);
321 node->offset = headers->offset;
324 headers = headers->next;
329 g_warning ("Invalid state encountered???: %u", camel_mime_parser_state (http->parser));
332 err = camel_mime_parser_errno (http->parser);
335 camel_object_unref (http->parser);
340 camel_mime_parser_drop_step (http->parser);
345 http_disconnect(http);
351 http_method_invoke (CamelHttpStream *http)
353 const char *method = NULL;
356 switch (http->method) {
357 case CAMEL_HTTP_METHOD_GET:
360 case CAMEL_HTTP_METHOD_HEAD:
364 g_assert_not_reached ();
367 url = camel_url_to_string (http->url, 0);
368 d(printf("HTTP Stream Sending: %s %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\n",
370 http->proxy ? url : http->url->path,
371 http->user_agent ? http->user_agent : "CamelHttpStream/1.0",
373 if (camel_stream_printf (http->raw, "%s %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\n",
375 http->proxy ? url : http->url->path,
376 http->user_agent ? http->user_agent : "CamelHttpStream/1.0",
377 http->url->host) == -1) {
378 http_disconnect(http);
385 d(printf("HTTP Stream Sending: WWW-Authenticate: %s\n", http->authrealm));
387 if (http->authrealm && camel_stream_printf (http->raw, "WWW-Authenticate: %s\r\n", http->authrealm) == -1) {
388 http_disconnect(http);
392 if (http->authpass && http->proxy)
393 d(printf("HTTP Stream Sending: Proxy-Aurhorization: Basic %s\n", http->authpass));
395 if (http->authpass && http->proxy && camel_stream_printf (http->raw, "Proxy-Authorization: Basic %s\r\n",
396 http->authpass) == -1) {
397 http_disconnect(http);
401 /* end the headers */
402 if (camel_stream_write (http->raw, "\r\n", 2) == -1 || camel_stream_flush (http->raw) == -1) {
403 http_disconnect(http);
411 stream_read (CamelStream *stream, char *buffer, size_t n)
413 CamelHttpStream *http = CAMEL_HTTP_STREAM (stream);
414 const char *parser_buf;
417 if (http->method != CAMEL_HTTP_METHOD_GET && http->method != CAMEL_HTTP_METHOD_HEAD) {
425 if (http_connect (http, http->proxy ? http->proxy : http->url) == NULL)
428 if (http_method_invoke (http) == -1)
431 if (http_get_statuscode (http) == -1)
434 if (http_get_headers (http) == -1)
437 switch (http->statuscode) {
440 /* we are OK to go... */
447 camel_content_type_unref (http->content_type);
448 http->content_type = NULL;
449 http_disconnect(http);
451 loc = g_strdup(camel_header_raw_find(&http->headers, "Location", NULL));
453 camel_header_raw_clear(&http->headers);
459 d(printf("HTTP redirect, location = %s\n", loc));
460 url = camel_url_new_with_base(http->url, loc);
461 camel_url_free (http->url);
464 http->url = camel_url_new(loc, NULL);
466 if (http->url == NULL) {
467 camel_header_raw_clear (&http->headers);
470 d(printf(" redirect url = %p\n", http->url));
471 camel_header_raw_clear (&http->headers);
476 /* failed proxy authentication? */
479 http_disconnect(http);
484 nread = camel_mime_parser_read (http->parser, &parser_buf, n);
487 memcpy (buffer, parser_buf, nread);
495 stream_write (CamelStream *stream, const char *buffer, size_t n)
501 stream_flush (CamelStream *stream)
503 CamelHttpStream *http = (CamelHttpStream *) stream;
506 return camel_stream_flush (http->raw);
512 stream_close (CamelStream *stream)
514 CamelHttpStream *http = (CamelHttpStream *) stream;
517 if (camel_stream_close (http->raw) == -1)
520 http_disconnect(http);
527 stream_reset (CamelStream *stream)
529 CamelHttpStream *http = CAMEL_HTTP_STREAM (stream);
532 http_disconnect(http);
538 camel_http_stream_get_content_type (CamelHttpStream *http_stream)
540 g_return_val_if_fail (CAMEL_IS_HTTP_STREAM (http_stream), NULL);
542 if (!http_stream->content_type && !http_stream->raw) {
543 if (http_connect (http_stream, http_stream->url) == NULL)
546 if (http_method_invoke (http_stream) == -1)
549 if (http_get_headers (http_stream) == -1)
553 if (http_stream->content_type)
554 camel_content_type_ref (http_stream->content_type);
556 return http_stream->content_type;
560 camel_http_stream_set_user_agent (CamelHttpStream *http_stream, const char *user_agent)
562 g_return_if_fail (CAMEL_IS_HTTP_STREAM (http_stream));
564 g_free (http_stream->user_agent);
565 http_stream->user_agent = g_strdup (user_agent);
569 camel_http_stream_set_proxy (CamelHttpStream *http_stream, const char *proxy_url)
571 g_return_if_fail (CAMEL_IS_HTTP_STREAM (http_stream));
573 if (http_stream->proxy)
574 camel_url_free (http_stream->proxy);
576 if (proxy_url == NULL)
577 http_stream->proxy = NULL;
579 http_stream->proxy = camel_url_new (proxy_url, NULL);
581 if (http_stream->proxy) {
582 char *basic, *basic64;
584 basic = g_strdup_printf("%s:%s", http_stream->proxy->user?http_stream->proxy->user:"",
585 http_stream->proxy->passwd?http_stream->proxy->passwd:"");
586 basic64 = camel_base64_encode_simple(basic, strlen(basic));
587 memset(basic, 0, strlen(basic));
589 camel_http_stream_set_proxy_authpass(http_stream, basic64);
590 memset(basic64, 0, strlen(basic64));
593 camel_http_stream_set_proxy_authpass(http_stream, NULL);
598 camel_http_stream_set_proxy_authrealm (CamelHttpStream *http_stream, const char *proxy_authrealm)
600 g_return_if_fail (CAMEL_IS_HTTP_STREAM (http_stream));
602 g_free (http_stream->authrealm);
603 http_stream->authrealm = g_strdup (proxy_authrealm);
607 camel_http_stream_set_proxy_authpass (CamelHttpStream *http_stream, const char *proxy_authpass)
609 g_return_if_fail (CAMEL_IS_HTTP_STREAM (http_stream));
611 g_free (http_stream->authpass);
612 http_stream->authpass = g_strdup (proxy_authpass);