xine-lib  1.2.9
http_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000-2011 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * URL helper functions
21  */
22 
23 #ifndef HTTP_HELPER_H
24 #define HTTP_HELPER_H
25 
26 #include <xine/attributes.h>
27 #include <xine/xine_internal.h>
28 
29 /*
30  * user agent finder, using modified protcol names
31  * {proto}://...
32  * e.g. "qthttp://example.com/foo.mov" → "QuickTime"
33  *
34  * return:
35  * NULL or user agent prefix
36  */
37 const char *_x_url_user_agent (const char *url);
38 
39 /*
40  * url parser
41  * {proto}://{user}:{password}@{host}:{port}{uri}
42  * {proto}://{user}:{password}@{[host]}:{port}{uri}
43  *
44  * return:
45  * 0 invalid url
46  * 1 valid url
47  */
48 int _x_parse_url (char *url, char **proto, char** host, int *port,
49  char **user, char **password, char **uri,
50  const char **user_agent);
51 
52 /*
53  * canonicalise url, given base
54  * base must be valid according to _x_parse_url
55  * url may only contain "://" if it's absolute
56  *
57  * return:
58  * the canonicalised URL (caller must free() it)
59  * NULL if error
60  */
61 static inline XINE_MALLOC char *_x_canonicalise_url (const char *base, const char *url) {
62 
63  size_t base_length;
64  char *cut;
65 
66  if ((cut = strstr (url, "://")))
67  return strdup (url);
68 
69  cut = strstr (base, "://");
70  _x_assert(cut); /* base is required to be valid according to _x_parse_url */
71 
72  if (url[0] == '/') {
73  /* absolute - base up to first '/' after "://", then url */
74  cut = cut ? strchr (cut + 3, '/') : NULL;
75  }
76  else {
77  /* relative - base up to & inc. last '/', then url */
78  cut = cut ? strrchr (cut, '/') : NULL;
79  if (cut)
80  ++cut;
81  }
82  base_length = cut ? (size_t)(cut - base) : strlen (base);
83 
84  return _x_asprintf ("%.*s%s", (int)base_length, base, url);
85 }
86 
87 #endif /* HTTP_HELPER_H */
#define XINE_MALLOC
Definition: attributes.h:119
int _x_parse_url(char *url, char **proto, char **host, int *port, char **user, char **password, char **uri, const char **user_agent)
Definition: http_helper.c:40
NULL
Definition: xine_plugin.c:91
char * _x_asprintf(const char *format,...)
Definition: utils.c:783
#define _x_assert(exp)
Definition: xineutils.h:463
static char * _x_canonicalise_url(const char *base, const char *url)
Definition: http_helper.h:61
const char * _x_url_user_agent(const char *url)
Definition: http_helper.c:33