Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
HTTPServer/HTTPFields.h@0:a548a085de55, 2010-03-14 (annotated)
- Committer:
- pehrhovey
- Date:
- Sun Mar 14 00:54:12 2010 +0000
- Revision:
- 0:a548a085de55
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pehrhovey | 0:a548a085de55 | 1 | #ifndef HTTPATTRIBUTE_H |
pehrhovey | 0:a548a085de55 | 2 | #define HTTPATTRIBUTE_H |
pehrhovey | 0:a548a085de55 | 3 | |
pehrhovey | 0:a548a085de55 | 4 | #include "HTTPServer.h" |
pehrhovey | 0:a548a085de55 | 5 | |
pehrhovey | 0:a548a085de55 | 6 | /** |
pehrhovey | 0:a548a085de55 | 7 | * A simple HTTPHandler which will add new fields to the httpresult header. |
pehrhovey | 0:a548a085de55 | 8 | * It can be used for adding caching strtegies. |
pehrhovey | 0:a548a085de55 | 9 | */ |
pehrhovey | 0:a548a085de55 | 10 | class HTTPFields : public HTTPHandler { |
pehrhovey | 0:a548a085de55 | 11 | public: |
pehrhovey | 0:a548a085de55 | 12 | /** |
pehrhovey | 0:a548a085de55 | 13 | * Create new HTTPFields |
pehrhovey | 0:a548a085de55 | 14 | * @param prefix The prefix path on witch we will execute the Handler. Means add the fields. |
pehrhovey | 0:a548a085de55 | 15 | * @param fields The Fields wicht are added to all Handlers with the same prefix and which are added after this one. |
pehrhovey | 0:a548a085de55 | 16 | */ |
pehrhovey | 0:a548a085de55 | 17 | HTTPFields(const char *prefix, const char *fields) : HTTPHandler(prefix) { _fields = fields; } |
pehrhovey | 0:a548a085de55 | 18 | HTTPFields(HTTPServer *server, const char *prefix, const char *fields) : HTTPHandler(prefix) { _fields = fields; server->addHandler(this); } |
pehrhovey | 0:a548a085de55 | 19 | |
pehrhovey | 0:a548a085de55 | 20 | private: |
pehrhovey | 0:a548a085de55 | 21 | /** |
pehrhovey | 0:a548a085de55 | 22 | * The Action methon should work on a Connection. |
pehrhovey | 0:a548a085de55 | 23 | * If the result is HTTP_AddFields the Server will know that we modified the connection header. |
pehrhovey | 0:a548a085de55 | 24 | * If the result is HTTP_Deliver the server will use this object to anwere the request. |
pehrhovey | 0:a548a085de55 | 25 | * |
pehrhovey | 0:a548a085de55 | 26 | * In this case we add new fields to the header. |
pehrhovey | 0:a548a085de55 | 27 | */ |
pehrhovey | 0:a548a085de55 | 28 | virtual HTTPHandle action(HTTPConnection *con) const { |
pehrhovey | 0:a548a085de55 | 29 | char *old = (char *)con->getHeaderFields(); |
pehrhovey | 0:a548a085de55 | 30 | int oldlen = strlen(old); |
pehrhovey | 0:a548a085de55 | 31 | int atrlen = strlen(_fields); |
pehrhovey | 0:a548a085de55 | 32 | char *fields = new char[atrlen+oldlen+3]; |
pehrhovey | 0:a548a085de55 | 33 | strcpy(fields,old); |
pehrhovey | 0:a548a085de55 | 34 | fields[oldlen+0] = '\r'; |
pehrhovey | 0:a548a085de55 | 35 | fields[oldlen+1] = '\n'; |
pehrhovey | 0:a548a085de55 | 36 | strcpy(&fields[oldlen+2], _fields); |
pehrhovey | 0:a548a085de55 | 37 | fields[atrlen+2+oldlen] = '\0'; |
pehrhovey | 0:a548a085de55 | 38 | con->setHeaderFields(fields); |
pehrhovey | 0:a548a085de55 | 39 | if(*old) { |
pehrhovey | 0:a548a085de55 | 40 | delete old; |
pehrhovey | 0:a548a085de55 | 41 | } |
pehrhovey | 0:a548a085de55 | 42 | return HTTP_AddFields; |
pehrhovey | 0:a548a085de55 | 43 | } |
pehrhovey | 0:a548a085de55 | 44 | |
pehrhovey | 0:a548a085de55 | 45 | const char *_fields; |
pehrhovey | 0:a548a085de55 | 46 | }; |
pehrhovey | 0:a548a085de55 | 47 | |
pehrhovey | 0:a548a085de55 | 48 | #endif |
pehrhovey | 0:a548a085de55 | 49 |