FAUST compiler  0.9.9.6b8
sourcefetcher.hh
Go to the documentation of this file.
1 /* http_fetcher.h - HTTP handling functions
2 
3  HTTP Fetcher
4  Copyright (C) 2001, 2003, 2004 Lyle Hanson (lhanson@users.sourceforge.net)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library 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 GNU
14  Library General Public License for more details.
15 
16  See LICENSE file for details
17 
18  */
19 
20 #ifndef HTTP_FETCHER_H
21 #define HTTP_FETCHER_H
22 
23 /* Error sources */
24 #define FETCHER_ERROR 0
25 #define ERRNO 1
26 #define H_ERRNO 2
27 
28 /* HTTP Fetcher error codes */
29 #define HF_SUCCESS 0
30 #define HF_METAERROR 1
31 #define HF_NULLURL 2
32 #define HF_HEADTIMEOUT 3
33 #define HF_DATATIMEOUT 4
34 #define HF_FRETURNCODE 5
35 #define HF_CRETURNCODE 6
36 #define HF_STATUSCODE 7
37 #define HF_CONTENTLEN 8
38 #define HF_HERROR 9
39 #define HF_CANTREDIRECT 10
40 #define HF_MAXREDIRECTS 11
41 
42 #define PORT_NUMBER 80
43 #define HTTP_VERSION "HTTP/1.0"
44 #define DEFAULT_USER_AGENT "HTTP Fetcher"
45 #define DEFAULT_READ_TIMEOUT 30 /* Seconds to wait before giving up
46  * when no data is arriving */
47 
48 #define REQUEST_BUF_SIZE 1024
49 #define HEADER_BUF_SIZE 1024
50 #define DEFAULT_PAGE_BUF_SIZE 1024 * 200 /* 200K should hold most things */
51 #define DEFAULT_REDIRECTS 3 /* Number of HTTP redirects to follow */
52 
53 
54 
55 /******************************************************************************/
56 /**************** Function declarations and descriptions **********************/
57 /******************************************************************************/
58 
59 /*
60  * [!!! NOTE !!!] All HTTP Fetcher functions return -1 on error. You can
61  * then either call http_perror to print the error message or call
62  * http_strerror to get a pointer to it
63  */
64 
65 
66  /*
67  * Download the page, registering a hit. If you pass it a NULL for fileBuf,
68  * 'url' will be requested but will not remain in memory (useful for
69  * simply registering a hit). Otherwise necessary space will be allocated
70  * and will be pointed to by fileBuf. Note that a NULL byte is added to
71  * the data, so the actual buffer will be the file size + 1.
72  * Returns:
73  * # of bytes downloaded, or
74  * -1 on error
75  */
76 int http_fetch(const char *url, char **fileBuf);
77 
78  /*
79  * Changes the User Agent (shown to the web server with each request)
80  * Send it NULL to avoid telling the server a User Agent
81  * By default, the User Agent is sent (The default one unless changed)
82  * Returns:
83  * 0 on success, or
84  * -1 on error (previous value for agent remains unchanged)
85  */
86 int http_setUserAgent(const char *newAgent);
87 
88  /*
89  * Changes the Referer (shown to the web server with each request)
90  * Send it NULL to avoid thelling the server a Referer
91  * By default, no Referer is sent
92  * Returns:
93  * 0 on success, or
94  * -1 on error
95  */
96 int http_setReferer(const char *newReferer);
97 
98  /*
99  * Changes the maximum amount of time that HTTP Fetcher will wait on
100  * data. If this many seconds elapses without more data from the
101  * server, http_fetch will return with an error.
102  * If you pass a value less than 0, reads will not time out, potentially
103  * waiting forever (or until data shows up, whichever comes first)
104  */
105 void http_setTimeout(int seconds);
106 
107  /*
108  * Changes the number of HTTP redirects HTTP Fetcher will automatically
109  * follow. If a request returns a status code of 3XX and contains
110  * a "Location:" field, the library will transparently follow up to
111  * the specified number of redirects. With this implementation
112  * (which is just a stopgap, really) the caller won't be aware of any
113  * redirection and will assume the returned document came from the original
114  * URL.
115  * To disable redirects, pass a 0. To follow unlimited redirects (probably
116  * unwise), pass a negative value. The default is to follow 3 redirects.
117  */
118 void http_setRedirects(int redirects);
119 
120  /*
121  * Takes a url and puts the filename portion of it into 'filename'.
122  * Returns:
123  * 0 on success, or
124  * 1 when url contains no end filename (i.e., "www.foo.com/")
125  * and **filename should not be assumed to point to anything), or
126  * -1 on error
127  */
128 int http_parseFilename(const char *url, char **filename);
129 
130  /*
131  * Works like perror. If an HTTP Fetcher function ever returns an
132  * error (-1), this will print a descriptive message to standard output
133  */
134 void http_perror(const char *string);
135 
136  /*
137  * Returns a pointer to the current error description message. The
138  * message pointed to is only good until the next call to http_strerror(),
139  * so if you need to hold on to the message for a while you should make
140  * a copy of it.
141  */
142 const char *http_strerror();
143 
144 
145 
146 /******************************************************************************/
147 /**** The following functions are used INTERNALLY by http_fetcher *************/
148 /******************************************************************************/
149 
150  /*
151  * Reads the metadata of an HTTP response. On success returns the number
152  * Returns:
153  * # of bytes read on success, or
154  * -1 on error
155  */
156 int _http_read_header(int sock, char *headerPtr);
157 
158  /*
159  * Opens a TCP socket and returns the descriptor
160  * Returns:
161  * socket descriptor, or
162  * -1 on error
163  */
164 int makeSocket(char *host);
165 
166  /*
167  * Determines if the given NULL-terminated buffer is large enough to
168  * concatenate the given number of characters. If not, it attempts to
169  * grow the buffer to fit.
170  * Returns:
171  * 0 on success, or
172  * -1 on error (original buffer is unchanged).
173  */
174 int _checkBufSize(char **buf, int *bufsize, int more);
175 
176 #endif
int _checkBufSize(char **buf, int *bufsize, int more)
int http_parseFilename(const char *url, char **filename)
void http_setTimeout(int seconds)
const char * http_strerror()
void http_perror(const char *string)
int http_setReferer(const char *newReferer)
void http_setRedirects(int redirects)
int _http_read_header(int sock, char *headerPtr)
int http_fetch(const char *url, char **fileBuf)
int makeSocket(char *host)
int http_setUserAgent(const char *newAgent)