1 |
/* wptHTTP.h - Generic network code for HTTP access |
2 |
* Copyright (C) 2004, 2005, 2006, 2007 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 |
#ifndef WPT_HTTP_H |
17 |
#define WPT_HTTP_H |
18 |
|
19 |
/* HTTP status codes. */ |
20 |
enum http_statcode_t { |
21 |
HTTP_STAT_NONE = 0, |
22 |
HTTP_STAT_200 = 200, /* OK */ |
23 |
HTTP_STAT_201 = 201, /* Created */ |
24 |
HTTP_STAT_301 = 301, /* Moved Permanently */ |
25 |
HTTP_STAT_302 = 302, /* Moved Temporarily */ |
26 |
HTTP_STAT_400 = 400, /* Bad Request */ |
27 |
HTTP_STAT_401 = 401, /* Unauthorized */ |
28 |
HTTP_STAT_403 = 403, /* Forbidden */ |
29 |
HTTP_STAT_404 = 404, /* Not Found */ |
30 |
HTTP_STAT_405 = 405, /* Method Not Allowed */ |
31 |
}; |
32 |
|
33 |
|
34 |
/* HTTP header list. */ |
35 |
struct http_head_s { |
36 |
struct http_head_s *next; |
37 |
char d[1]; |
38 |
}; |
39 |
typedef struct http_head_s *http_head_t; |
40 |
|
41 |
class NetHTTP { |
42 |
public: |
43 |
/* HTTP methods. */ |
44 |
enum { HTTP_GET, HTTP_HEAD, HTTP_PUT, HTTP_POST }; |
45 |
|
46 |
private: |
47 |
http_head_t req_headers; |
48 |
http_head_t resp_headers; |
49 |
int fd; |
50 |
int statcode; |
51 |
char *host; |
52 |
int port; |
53 |
char *url; |
54 |
int nleft; |
55 |
int method; |
56 |
int ver; |
57 |
int error; |
58 |
|
59 |
private: |
60 |
void reset (void); |
61 |
int parseResponse (int *statcode); |
62 |
int connect (const char *host, int port); |
63 |
int isDataAvailable (int fd); |
64 |
int readLine (char *buf, unsigned int nbuf, int nonblock, int *nn, int *eof); |
65 |
void addHeader (http_head_t *root, const char *val); |
66 |
bool findHeader (http_head_t root, |
67 |
const char *name, const char **val); |
68 |
int parseHeaders (http_head_t *r_head); |
69 |
|
70 |
int extractHostInfo (const char *url, char **host, char **new_url); |
71 |
int sendRequest (const char *host, int port, const char *url); |
72 |
|
73 |
public: |
74 |
NetHTTP (); |
75 |
NetHTTP (const char *url); |
76 |
NetHTTP (const char *host, int port, const char *url); |
77 |
~NetHTTP (); |
78 |
|
79 |
void setVersion (int ver); |
80 |
int getErrorCode (void); |
81 |
int getStatusCode (void); |
82 |
unsigned int getContentLength (void); |
83 |
const char *getContentType (void); |
84 |
|
85 |
int addRequestHeader (const char *name, const char *val); |
86 |
int addRequestHeader (const char *name, unsigned int val); |
87 |
|
88 |
int open (const char *url); |
89 |
int write (const void *buf, unsigned int buflen); |
90 |
int read (void *buf, unsigned int buflen); |
91 |
int readData (FILE *out); |
92 |
|
93 |
int get (const char *url); |
94 |
int head (const char *url); |
95 |
}; |
96 |
|
97 |
#endif /* WPT_HTTP_H */ |