util.h 4.07 KB
Newer Older
1 2
/* Icecast
 *
3
 *  Copyright 2000-2004 Jack Moffitt <jack@xiph.org, 
4 5 6 7
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
8 9 10 11
 *  Copyright 2012-2015 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
 *
 *  This program is distributed under the GNU General Public License, version 2.
 *  A copy of this license is included with this source.
12 13
 */

Jack Moffitt's avatar
Jack Moffitt committed
14 15 16
#ifndef __UTIL_H__
#define __UTIL_H__

17 18 19
/* for FILE* */
#include <stdio.h>

20 21 22
#define UNKNOWN_CONTENT 0
#define XSLT_CONTENT    1
#define HTML_CONTENT    2
23

24 25 26
#define READ_ENTIRE_HEADER 1
#define READ_LINE 0

27 28
#define MAX_LINE_LEN 512

29 30
int util_timed_wait_for_fd(sock_t fd, int timeout);
int util_read_header(sock_t sock, char *buff, unsigned long len, int entire);
31
int util_check_valid_extension(const char *uri);
32
char *util_get_extension(const char *path);
33
char *util_get_path_from_uri(char *uri);
34
char *util_get_path_from_normalised_uri(const char *uri);
35
char *util_normalise_uri(const char *uri);
36
char *util_base64_encode(const char *data, size_t len);
37
char *util_base64_decode(const char *input);
38
char *util_bin_to_hex(unsigned char *data, int len);
Jack Moffitt's avatar
Jack Moffitt committed
39

40 41 42 43 44 45 46 47 48 49 50 51
typedef enum _util_hostcheck_tag {
 HOSTCHECK_ERROR = -1,
 HOSTCHECK_SANE  = 0,
 HOSTCHECK_NOT_FQDN,
 HOSTCHECK_IS_LOCALHOST,
 HOSTCHECK_IS_IPV4,
 HOSTCHECK_IS_IPV6,
 HOSTCHECK_BADCHAR
} util_hostcheck_type;

util_hostcheck_type util_hostcheck(const char *hostname);

52
int util_str_to_bool(const char *str);
53
int util_str_to_loglevel(const char *str);
54 55
int util_str_to_int(const char *str, const int default_value);
unsigned int util_str_to_unsigned_int(const char *str, const unsigned int default_value);
56

57 58
char *util_url_unescape(const char *src);
char *util_url_escape(const char *src);
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* Function to build up a HTTP header.
 * out is the pointer to storage.
 * len is the total length of out.
 * offset is the offset in out we should start writing at if offset >= 0.
 * In this case the 'used' length is len-offset.
 * If offset is -1 we append at the end of out.
 * In this case the used length is len-strlen(out).
 * cache is a bool. If set allow the client to cache the content (static data).
 * if unset we will send no-caching headers.
 * status is the HTTP status code. If -1 no HTTP status header is generated.
 * statusmsg is the status header text. If NULL a default one
 * is used for the given status code. This is ignored if status is -1.
 * contenttype is the value for the Content-Type header.
 * if NULL no such header is generated.
 * charset is the charset for the object. If NULL no header is generated
 * or default is used.
 * datablock is random data added to the request.
 * If datablock is non NULL the end-of-header is appended as well as this datablock.
 * If datablock is NULL no end-of-header nor any data is appended.
 * Returns the number of bytes written or -1 on error.
 */
81 82
struct source_tag; /* use forward decleration so we do not need to
                    * include <source.h> that would cause other conflicts. */
83 84 85 86
ssize_t util_http_build_header(char * out, size_t len, ssize_t offset,
        int cache,
        int status, const char * statusmsg,
        const char * contenttype, const char * charset,
87
        const char * datablock,
88 89
        struct source_tag * source,
        struct _client_tag * client);
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* String dictionary type, without support for NULL keys, or multiple
 * instances of the same key */
typedef struct _util_dict {
  char *key;
  char *val;
  struct _util_dict *next;
} util_dict;

util_dict *util_dict_new(void);
void util_dict_free(util_dict *dict);
/* dict, key must not be NULL. */
int util_dict_set(util_dict *dict, const char *key, const char *val);
const char *util_dict_get(util_dict *dict, const char *key);
char *util_dict_urlencode(util_dict *dict, char delim);
105 106

#ifndef HAVE_LOCALTIME_R
107
struct tm *localtime_r(const time_t *timep, struct tm *result);
108
#endif
109
char *util_conv_string (const char *string, const char *in_charset, const char *out_charset);
110

111
int get_line(FILE *file, char *buf, size_t siz);
Jack Moffitt's avatar
Jack Moffitt committed
112
#endif  /* __UTIL_H__ */