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	5		// Seconds to wait before giving up when no data is arriving
46 
47 #define REQUEST_BUF_SIZE 		1024
48 #define HEADER_BUF_SIZE 		1024
49 #define DEFAULT_PAGE_BUF_SIZE   (1024 * 200)    /* 200K should hold most things */
50 #define DEFAULT_REDIRECTS       3               /* Number of HTTP redirects to follow */
51 
52 /******************************************************************************/
53 /**************** Function declarations and descriptions **********************/
54 /******************************************************************************/
55 
56 /*
57  * [!!! NOTE !!!]  All HTTP Fetcher functions return -1 on error.  You can
58  *	then either call http_perror to print the error message or call
59  *	http_strerror to get a pointer to it
60  */
61 
62 	/*
63 	 * Download the page, registering a hit. If you pass it a NULL for fileBuf,
64 	 *  'url' will be requested but will not remain in memory (useful for
65 	 *  simply registering a hit).  Otherwise necessary space will be allocated
66 	 *  and will be pointed to by fileBuf, and has to be dealoocated after use.
67      *  Note that a NULL byte is added to the data, so the actual buffer
68      *  will be the file size + 1.
69 	 * Returns:
70 	 *	# of bytes downloaded, or
71 	 *	-1 on error
72 	 */
73 int http_fetch(const char *url, char **fileBuf);
74 
75 	/*
76 	 * Changes the User Agent (shown to the web server with each request)
77 	 *	Send it NULL to avoid telling the server a User Agent
78 	 *	By default, the User Agent is sent (The default one unless changed)
79 	 * Returns:
80 	 *	0 on success, or
81 	 *	-1 on error (previous value for agent remains unchanged)
82 	 */
83 int http_setUserAgent(const char *newAgent);
84 
85 	/*
86 	 * Changes the Referer (shown to the web server with each request)
87 	 *	Send it NULL to avoid thelling the server a Referer
88 	 *	By default, no Referer is sent
89 	 * Returns:
90 	 *	0 on success, or
91 	 *	-1 on error
92 	 */
93 int http_setReferer(const char *newReferer);
94 
95 	/*
96 	 * Changes the maximum amount of time that HTTP Fetcher will wait on
97 	 *	data.  If this many seconds elapses without more data from the
98 	 *	server, http_fetch will return with an error.
99 	 * If you pass a value less than 0, reads will not time out, potentially
100 	 *	waiting forever (or until data shows up, whichever comes first)
101 	 */
102 void http_setTimeout(int seconds);
103 
104 	/*
105 	 * Changes the number of HTTP redirects HTTP Fetcher will automatically
106 	 *	follow.  If a request returns a status code of 3XX and contains
107 	 *	a "Location:" field, the library will transparently follow up to
108 	 *	the specified number of redirects.  With this implementation
109 	 *	(which is just a stopgap, really) the caller won't be aware of any
110 	 *	redirection and will assume the returned document came from the original
111 	 *	URL.
112 	 * To disable redirects, pass a 0.  To follow unlimited redirects (probably
113 	 *  unwise), pass a negative value.  The default is to follow 3 redirects.
114 	 */
115 void http_setRedirects(int redirects);
116 
117 	/*
118 	 * Takes a url and puts the filename portion of it into 'filename'.
119 	 * Returns:
120 	 *	0 on success, or
121 	 *	1 when url contains no end filename (i.e., "www.foo.com/")
122 	 *	and **filename should not be assumed to point to anything), or
123 	 *	-1 on error
124 	 */
125 int http_parseFilename(const char *url, char **filename);
126 
127 	/*
128 	 * Works like perror.  If an HTTP Fetcher function ever returns an
129 	 *	error (-1), this will print a descriptive message to standard output
130 	 */
131 void http_perror(const char *string);
132 
133 	/*
134 	 * Returns a pointer to the current error description message.  The
135 	 *	message pointed to is only good until the next call to http_strerror(),
136 	 *	so if you need to hold on to the message for a while you should make
137 	 *	a copy of it.
138 	 */
139 const char *http_strerror();
140 
141 /******************************************************************************/
142 /**** The following functions are used INTERNALLY by http_fetcher *************/
143 /******************************************************************************/
144 
145 	/*
146 	 * Reads the metadata of an HTTP response.  On success returns the number
147 	 * Returns:
148 	 *	# of bytes read on success, or
149 	 *	-1 on error
150 	 */
151 int _http_read_header(int sock, char *headerPtr);
152 
153 	/*
154 	 * Opens a TCP socket and returns the descriptor
155 	 * Returns:
156 	 *	socket descriptor, or
157 	 *	-1 on error
158 	 */
159 int makeSocket(char *host);
160 
161 	/*
162 	 * Determines if the given NULL-terminated buffer is large enough to
163 	 *	concatenate the given number of characters.  If not, it attempts to
164 	 *	grow the buffer to fit.
165 	 * Returns:
166 	 *	0 on success, or
167 	 *	-1 on error (original buffer is unchanged).
168 	 */
169 int _checkBufSize(char **buf, int *bufsize, int more);
170 
171 #endif
172