1 |
/* wptHttp.h - Generic HTTP code |
2 |
* Copyright (C) 2004, 2005, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* WinPT 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 |
* General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#ifndef WPT_HTTP_H |
22 |
#define WPT_HTTP_H |
23 |
|
24 |
/* HTTP status codes. */ |
25 |
enum http_statcode_t { |
26 |
HTTP_STAT_NONE = 0, |
27 |
HTTP_STAT_200 = 200, /* OK */ |
28 |
HTTP_STAT_201 = 201, /* Created */ |
29 |
HTTP_STAT_301 = 301, /* Moved Permanently */ |
30 |
HTTP_STAT_302 = 302, /* Moved Temporarily */ |
31 |
HTTP_STAT_400 = 400, /* Bad Request */ |
32 |
HTTP_STAT_401 = 401, /* Unauthorized */ |
33 |
HTTP_STAT_403 = 403, /* Forbidden */ |
34 |
HTTP_STAT_404 = 404, /* Not Found */ |
35 |
HTTP_STAT_405 = 405, /* Method Not Allowed */ |
36 |
}; |
37 |
|
38 |
|
39 |
/* HTTP header list. */ |
40 |
struct http_head_s { |
41 |
struct http_head_s * next; |
42 |
int flags; |
43 |
char d[1]; |
44 |
}; |
45 |
typedef struct http_head_s *http_head_t; |
46 |
|
47 |
class wHTTP { |
48 |
public: |
49 |
/* HTTP methods. */ |
50 |
enum { HTTP_GET, HTTP_HEAD, HTTP_PUT, HTTP_POST }; |
51 |
|
52 |
private: |
53 |
http_head_t req_headers; |
54 |
http_head_t resp_headers; |
55 |
int fd; |
56 |
int statcode; |
57 |
char *host; |
58 |
int port; |
59 |
char *url; |
60 |
int nleft; |
61 |
int method; |
62 |
int ver; |
63 |
int error; |
64 |
|
65 |
private: |
66 |
void reset (void); |
67 |
int parseResponse (int *statcode); |
68 |
int connect (const char *host, int port); |
69 |
int isDataAvailable (int fd); |
70 |
int readLine (char *buf, unsigned int nbuf, int nonblock, int *nn, int *eof); |
71 |
void addHeader (http_head_t *root, const char *val); |
72 |
bool findHeader (http_head_t root, |
73 |
const char *name, const char **val); |
74 |
int parseHeaders (http_head_t *r_head); |
75 |
|
76 |
int extractHostInfo (const char *url, char **host, char **new_url); |
77 |
int sendRequest (const char *host, int port, const char *url); |
78 |
|
79 |
public: |
80 |
wHTTP (); |
81 |
wHTTP (const char *url); |
82 |
wHTTP (const char *host, int port, const char *url); |
83 |
~wHTTP (); |
84 |
|
85 |
void setVersion (int ver); |
86 |
int getErrorCode (void); |
87 |
int getStatusCode (void); |
88 |
unsigned int getContentLength (void); |
89 |
const char *getContentType (void); |
90 |
|
91 |
int addRequestHeader (const char *name, const char *val); |
92 |
int addRequestHeader (const char *name, unsigned int val); |
93 |
|
94 |
int write (const void *buf, unsigned int buflen); |
95 |
int read (void *buf, unsigned int buflen); |
96 |
int readData (FILE *out); |
97 |
|
98 |
int get (const char *url); |
99 |
int head (const char *url); |
100 |
}; |
101 |
|
102 |
#endif /* WPT_HTTP_H */ |