My fork of the HTTPServer (working)

Dependents:   DGWWebServer LAN2

Committer:
screamer
Date:
Tue Nov 20 12:18:53 2012 +0000
Revision:
1:284f2df30cf9
Parent:
0:7a64fbb4069d
local changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:7a64fbb4069d 1 #ifndef HTTPCLIENT_H
screamer 0:7a64fbb4069d 2 #define HTTPCLIENT_H
screamer 0:7a64fbb4069d 3
screamer 0:7a64fbb4069d 4 #include "TCPConnection.h"
screamer 0:7a64fbb4069d 5 #include "NetServer.h"
screamer 0:7a64fbb4069d 6
screamer 0:7a64fbb4069d 7 /* Class: HTTPClient
screamer 0:7a64fbb4069d 8 * A simple Class to fetch HTTP Pages.
screamer 0:7a64fbb4069d 9 */
screamer 0:7a64fbb4069d 10 class HTTPClient : public TCPConnection {
screamer 0:7a64fbb4069d 11 public:
screamer 0:7a64fbb4069d 12 /* Constructor: HTTPClient
screamer 0:7a64fbb4069d 13 * Creates an HTTPClient object. You might want to initialise the network server befor.
screamer 0:7a64fbb4069d 14 * If you dont do it it will be happen by the first post or get request you make.
screamer 0:7a64fbb4069d 15 */
screamer 0:7a64fbb4069d 16 HTTPClient() : TCPConnection(), _auth(NULL), _timeout(0), _data(NULL), _headerfields(NULL) {}
screamer 0:7a64fbb4069d 17
screamer 0:7a64fbb4069d 18 /* Destructor: ~HTTPClient
screamer 0:7a64fbb4069d 19 * Destroys the HTTPClient class.
screamer 0:7a64fbb4069d 20 */
screamer 0:7a64fbb4069d 21 virtual ~HTTPClient() {
screamer 0:7a64fbb4069d 22 if(_auth) {
screamer 0:7a64fbb4069d 23 delete _auth;
screamer 0:7a64fbb4069d 24 _auth = NULL;
screamer 0:7a64fbb4069d 25 }
screamer 0:7a64fbb4069d 26 }
screamer 0:7a64fbb4069d 27
screamer 0:7a64fbb4069d 28 /* Function: headers
screamer 0:7a64fbb4069d 29 * Add header additional Information to the next post or get requests.
screamer 0:7a64fbb4069d 30 * Somtimes it is useful to add further header information. For example an auth field.
screamer 0:7a64fbb4069d 31 * Each time you call this function it will be replace the header fields given by an
screamer 0:7a64fbb4069d 32 * prior call.
screamer 0:7a64fbb4069d 33 *
screamer 0:7a64fbb4069d 34 * It will not free your data.
screamer 0:7a64fbb4069d 35 * Variables:
screamer 0:7a64fbb4069d 36 * fields - A string containing all fields you want to add. Seperated by "\\r\\n".
screamer 0:7a64fbb4069d 37 *
screamer 0:7a64fbb4069d 38 */
screamer 0:7a64fbb4069d 39 void headers(const char *fields);
screamer 0:7a64fbb4069d 40
screamer 0:7a64fbb4069d 41 /* Function: auth
screamer 0:7a64fbb4069d 42 * Enables basic authentication. Just enter username and password
screamer 0:7a64fbb4069d 43 * and they will be used for all requests.
screamer 0:7a64fbb4069d 44 * If you want to lean your username and passwort just insert NULL, NULL.
screamer 0:7a64fbb4069d 45 *
screamer 0:7a64fbb4069d 46 * Variables:
screamer 0:7a64fbb4069d 47 * user - Username for ure auth or NULL.
screamer 0:7a64fbb4069d 48 * password - Password for auth or NULL.
screamer 0:7a64fbb4069d 49 */
screamer 0:7a64fbb4069d 50 void auth(const char *user, const char *password);
screamer 0:7a64fbb4069d 51
screamer 0:7a64fbb4069d 52 /* Function: get
screamer 0:7a64fbb4069d 53 * A simple get-request just insert the url.
screamer 0:7a64fbb4069d 54 * But if you want you can get the result back as a string.
screamer 0:7a64fbb4069d 55 * Sometimes you want get a large result, more than 64 Bytes
screamer 0:7a64fbb4069d 56 * than define your size.
screamer 0:7a64fbb4069d 57 *
screamer 0:7a64fbb4069d 58 * Variables:
screamer 0:7a64fbb4069d 59 * url - The requested URL.
screamer 0:7a64fbb4069d 60 * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes.
screamer 0:7a64fbb4069d 61 * rsize - The maximum size of your result. By default 64 Bytes.
screamer 0:7a64fbb4069d 62 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
screamer 0:7a64fbb4069d 63 */
screamer 0:7a64fbb4069d 64 unsigned int get(const char *url, char *result = NULL, int rsize = 64);
screamer 0:7a64fbb4069d 65
screamer 0:7a64fbb4069d 66 /* Function: get
screamer 0:7a64fbb4069d 67 * A simple get-request just insert the url and a FILE Pointer.
screamer 0:7a64fbb4069d 68 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data.
screamer 0:7a64fbb4069d 69 *
screamer 0:7a64fbb4069d 70 * Variables:
screamer 0:7a64fbb4069d 71 * url - The requested URL.
screamer 0:7a64fbb4069d 72 * result - The FILE Pointer in which you want to store the result.
screamer 0:7a64fbb4069d 73 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
screamer 0:7a64fbb4069d 74 */
screamer 0:7a64fbb4069d 75 unsigned int get(const char *url, FILE *result);
screamer 0:7a64fbb4069d 76
screamer 0:7a64fbb4069d 77 /* Function: post
screamer 0:7a64fbb4069d 78 * A simple post-request just insert the url.
screamer 0:7a64fbb4069d 79 * You can send data if you want but they should be NULL-Terminated.
screamer 0:7a64fbb4069d 80 * If you want you can get the result back as a string.
screamer 0:7a64fbb4069d 81 * Sometimes you want get a large result, more than 64 Bytes
screamer 0:7a64fbb4069d 82 * than define your size.
screamer 0:7a64fbb4069d 83 *
screamer 0:7a64fbb4069d 84 * Variables:
screamer 0:7a64fbb4069d 85 * url - The requested URL.
screamer 0:7a64fbb4069d 86 * data - A char array of the data you might want to send.
screamer 0:7a64fbb4069d 87 * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes.
screamer 0:7a64fbb4069d 88 * rsize - The maximum size of your result. By default 64 Bytes.
screamer 0:7a64fbb4069d 89 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
screamer 0:7a64fbb4069d 90 */
screamer 0:7a64fbb4069d 91 unsigned int post(const char *url, const char *data = NULL, char *result = NULL, int rsize = 64);
screamer 0:7a64fbb4069d 92
screamer 0:7a64fbb4069d 93 /* Function: post
screamer 0:7a64fbb4069d 94 * A simple get-request just insert the url and a FILE Pointer.
screamer 0:7a64fbb4069d 95 * You can send data if you want but they should be NULL-Terminated.
screamer 0:7a64fbb4069d 96 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data.
screamer 0:7a64fbb4069d 97 *
screamer 0:7a64fbb4069d 98 * Variables:
screamer 0:7a64fbb4069d 99 * url - The requested URL.
screamer 0:7a64fbb4069d 100 * data - A char array of the data you might want to send.
screamer 0:7a64fbb4069d 101 * result - The FILE Pointer in which you want to store the result.
screamer 0:7a64fbb4069d 102 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
screamer 0:7a64fbb4069d 103 */
screamer 0:7a64fbb4069d 104 unsigned int post(const char *url, const char *data, FILE *result);
screamer 0:7a64fbb4069d 105
screamer 0:7a64fbb4069d 106 /* Function: post
screamer 0:7a64fbb4069d 107 * A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results.
screamer 0:7a64fbb4069d 108 * Your data to sent can come from a file.
screamer 0:7a64fbb4069d 109 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data.
screamer 0:7a64fbb4069d 110 *
screamer 0:7a64fbb4069d 111 * Variables:
screamer 0:7a64fbb4069d 112 * url - The requested URL.
screamer 0:7a64fbb4069d 113 * data - A FILE Pointer of a file you might want to send.
screamer 0:7a64fbb4069d 114 * result - The FILE Pointer in which you want to store the result.
screamer 0:7a64fbb4069d 115 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
screamer 0:7a64fbb4069d 116 */
screamer 0:7a64fbb4069d 117 unsigned int post(const char *url, FILE *data, FILE *result);
screamer 0:7a64fbb4069d 118
screamer 0:7a64fbb4069d 119 /* Function: post
screamer 0:7a64fbb4069d 120 * A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results.
screamer 0:7a64fbb4069d 121 * Your data to sent can come from a file.
screamer 0:7a64fbb4069d 122 * If you want you can get the result back as a string.
screamer 0:7a64fbb4069d 123 * Sometimes you want get a large result, more than 64 Bytes
screamer 0:7a64fbb4069d 124 * than define your size.
screamer 0:7a64fbb4069d 125 *
screamer 0:7a64fbb4069d 126 * url - The requested URL.
screamer 0:7a64fbb4069d 127 * data - A FILE Pointer of a file you might want to send.
screamer 0:7a64fbb4069d 128 * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes.
screamer 0:7a64fbb4069d 129 * length - The maximum size of your result. By default 64 Bytes.
screamer 0:7a64fbb4069d 130 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
screamer 0:7a64fbb4069d 131 */
screamer 0:7a64fbb4069d 132 unsigned int post(const char *url, FILE *data = NULL, char *result = NULL, int length = 64);
screamer 0:7a64fbb4069d 133
screamer 0:7a64fbb4069d 134 private:
screamer 0:7a64fbb4069d 135 virtual void err(err_t err);
screamer 0:7a64fbb4069d 136 virtual err_t poll();
screamer 0:7a64fbb4069d 137 virtual err_t sent(u16_t len) {return ERR_OK;};
screamer 0:7a64fbb4069d 138 virtual err_t connected(err_t err);
screamer 0:7a64fbb4069d 139 virtual err_t recv(struct pbuf *q, err_t err);
screamer 0:7a64fbb4069d 140 virtual void dnsreply(const char *hostname, struct ip_addr *ipaddr);
screamer 0:7a64fbb4069d 141 unsigned int make(const char *);
screamer 0:7a64fbb4069d 142
screamer 0:7a64fbb4069d 143 char *_auth;
screamer 0:7a64fbb4069d 144 bool _ready;
screamer 0:7a64fbb4069d 145 char _mode;
screamer 0:7a64fbb4069d 146 char _state;
screamer 0:7a64fbb4069d 147 int _timeout;
screamer 0:7a64fbb4069d 148 const char *_host;
screamer 0:7a64fbb4069d 149 const char *_path;
screamer 0:7a64fbb4069d 150 void *_result;
screamer 0:7a64fbb4069d 151 void *_data;
screamer 0:7a64fbb4069d 152 const char *_request;
screamer 0:7a64fbb4069d 153 const char *_headerfields;
screamer 0:7a64fbb4069d 154 unsigned int _hostlen;
screamer 0:7a64fbb4069d 155 unsigned int _resultoff;
screamer 0:7a64fbb4069d 156 unsigned int _resultleft;
screamer 0:7a64fbb4069d 157 };
screamer 0:7a64fbb4069d 158
screamer 0:7a64fbb4069d 159 #endif