1 |
/* wptHttp.h - Generic HTTP code |
/* wptHTTP.h - Generic network code for HTTP access |
2 |
* Copyright (C) 2004, 2005 Timo Schulz |
* Copyright (C) 2004, 2005, 2006, 2007 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
15 |
* |
*/ |
16 |
* You should have received a copy of the GNU General Public License |
#ifndef WPT_HTTP_H |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
#define WPT_HTTP_H |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
19 |
*/ |
/* HTTP status codes. */ |
20 |
|
enum http_statcode_t { |
21 |
#ifndef WPT_HTTP_H |
HTTP_STAT_NONE = 0, |
22 |
#define WPT_HTTP_H |
HTTP_STAT_200 = 200, /* OK */ |
23 |
|
HTTP_STAT_201 = 201, /* Created */ |
24 |
#ifdef __cplusplus |
HTTP_STAT_301 = 301, /* Moved Permanently */ |
25 |
extern "C" { |
HTTP_STAT_302 = 302, /* Moved Temporarily */ |
26 |
#endif |
HTTP_STAT_400 = 400, /* Bad Request */ |
27 |
|
HTTP_STAT_401 = 401, /* Unauthorized */ |
28 |
/* HTTP methods. */ |
HTTP_STAT_403 = 403, /* Forbidden */ |
29 |
enum http_types_t { |
HTTP_STAT_404 = 404, /* Not Found */ |
30 |
HTTP_NONE = 0, |
HTTP_STAT_405 = 405, /* Method Not Allowed */ |
31 |
HTTP_GET, |
}; |
32 |
HTTP_POST, |
|
33 |
}; |
|
34 |
|
/* HTTP header list. */ |
35 |
/* HTTP status codes. */ |
struct http_head_s { |
36 |
enum http_statcode_t { |
struct http_head_s *next; |
37 |
HTTP_STAT_NONE = 0, |
char d[1]; |
38 |
HTTP_STAT_200 = 200, /* OK */ |
}; |
39 |
HTTP_STAT_301 = 301, /* Moved Permanently */ |
typedef struct http_head_s *http_head_t; |
40 |
HTTP_STAT_302 = 302, /* Moved Temporarily */ |
|
41 |
HTTP_STAT_400 = 400, /* Bad Request */ |
class NetHTTP { |
42 |
HTTP_STAT_403 = 403, /* Forbidden */ |
public: |
43 |
HTTP_STAT_404 = 404, /* Not Found */ |
/* HTTP methods. */ |
44 |
HTTP_STAT_405 = 405, /* Method Not Allowed */ |
enum { HTTP_GET, HTTP_HEAD, HTTP_PUT, HTTP_POST }; |
45 |
}; |
|
46 |
|
private: |
47 |
/* HTTP header list. */ |
http_head_t req_headers; |
48 |
struct http_head_s { |
http_head_t resp_headers; |
49 |
struct http_head_s * next; |
int fd; |
50 |
int flags; |
int statcode; |
51 |
char d[1]; |
char *host; |
52 |
}; |
int port; |
53 |
typedef struct http_head_s * http_head_t; |
char *url; |
54 |
|
int nleft; |
55 |
/* HTTP handle. */ |
int method; |
56 |
struct http_hd_s { |
int ver; |
57 |
int fd; |
int error; |
58 |
http_head_t head; |
|
59 |
}; |
private: |
60 |
typedef struct http_hd_s * http_hd_t; |
void reset (void); |
61 |
|
int parseResponse (int *statcode); |
62 |
/* HTTP request. */ |
int connect (const char *host, int port); |
63 |
struct http_req_s { |
int isDataAvailable (int fd); |
64 |
int type; |
int readLine (char *buf, unsigned int nbuf, int nonblock, int *nn, int *eof); |
65 |
http_head_t head; |
void addHeader (http_head_t *root, const char *val); |
66 |
char * url; |
bool findHeader (http_head_t root, |
67 |
}; |
const char *name, const char **val); |
68 |
typedef struct http_req_s * http_req_t; |
int parseHeaders (http_head_t *r_head); |
69 |
|
|
70 |
int http_send_request (const char * host, int port, const char* url, |
int extractHostInfo (const char *url, char **host, char **new_url); |
71 |
http_hd_t * r_hd); |
int sendRequest (const char *host, int port, const char *url); |
72 |
int http_send_request2 (const char * url, http_hd_t * r_hd); |
|
73 |
int http_parse_response (http_hd_t hd, int * statcode); |
public: |
74 |
int http_parse_data (http_hd_t hd, FILE * out); |
NetHTTP (); |
75 |
int http_hd_new (http_hd_t * hd); |
NetHTTP (const char *url); |
76 |
void http_hd_free (http_hd_t hd); |
NetHTTP (const char *host, int port, const char *url); |
77 |
int http_req_new (http_req_t * ctx); |
~NetHTTP (); |
78 |
void http_req_free (http_req_t ctx); |
|
79 |
int http_parse_request (http_hd_t hd, http_req_t * r_req); |
void setVersion (int ver); |
80 |
|
int getErrorCode (void); |
81 |
#ifdef __cplusplus |
int getStatusCode (void); |
82 |
} |
unsigned int getContentLength (void); |
83 |
#endif |
const char *getContentType (void); |
84 |
|
|
85 |
#endif /* WPT_HTTP_H */ |
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 */ |