61fe2c75eb8e1463a9a59a9bdad36099b3a3424b
[enigma2.git] / lib / network / httpd.h
1 #ifndef __httpd_h
2 #define __httpd_h
3
4 #include <asm/types.h>
5 #include <map>
6
7 #include <lib/base/eptrlist.h>
8 #include <lib/base/ebase.h>
9 #include <string>
10 #include <lib/base/eerror.h>
11 #include <lib/network/socket.h>
12 #include <lib/network/serversocket.h>
13
14 class eHTTPConnection;
15 class eHTTPDataSource;
16 class eHTTPD;
17
18 class eHTTPPathResolver
19 {
20 public:
21         virtual ~eHTTPPathResolver() {}; 
22         virtual eHTTPDataSource *getDataSource(std::string request, std::string path, eHTTPConnection *conn)=0;
23 };
24
25 class eHTTPDataSource
26 {
27 protected:
28         eHTTPConnection *connection;
29 public:
30         eHTTPDataSource(eHTTPConnection *c);
31         virtual ~eHTTPDataSource();
32         virtual void haveData(void *data, int len);
33         virtual int doWrite(int bytes); // number of written bytes, -1 for "no more"
34 };
35
36 class eHTTPError: public eHTTPDataSource
37 {
38         int errcode;
39 public:
40         eHTTPError(eHTTPConnection *c, int errcode);
41         ~eHTTPError() { }
42         void haveData();
43         int doWrite(int bytes);
44 };
45
46 class eHTTPConnection: public eSocket
47 {
48         void doError(int error);
49         
50         int getLine(std::string &line);
51         
52         int processLocalState();
53         int processRemoteState();
54         void writeString(const char *data);
55         
56         eHTTPDataSource *data;
57         eHTTPD *parent;
58         
59         int buffersize;
60 private:
61         void readData();
62         void gotError(int);
63         void bytesWritten(int);
64         void hostConnected();
65         void destruct();
66 public:
67         Signal1<void,int> transferDone;
68         Signal1<eHTTPDataSource*,eHTTPConnection*> createDataSource;
69         enum
70         {
71                 /*
72                 
73                 < GET / HTTP/1.0
74                 < If-modified-since: bla
75                 <
76                 < Data
77                 > 200 OK HTTP/1.0
78                 > Content-Type: text/html
79                 >
80                 > Data
81                 */
82         
83                 stateWait, stateRequest, stateResponse, stateHeader, stateData, stateDone, stateClose
84         };
85         int localstate, remotestate;
86         int persistent;
87         
88         eHTTPConnection(int socket, int issocket, eHTTPD *parent, int persistent=0);
89         eHTTPConnection(eMainloop *ml); // ready to do "connectToHost"
90         static eHTTPConnection *doRequest(const char *uri, eMainloop *ml, int *error=0);
91         void start();
92         void gotHangup();
93         ~eHTTPConnection();
94         
95                 // stateRequest
96         std::string request, requestpath, httpversion;
97         int is09;
98         
99                 // stateResponse
100         
101         int code;
102         std::string code_descr;
103         
104         std::map<std::string,std::string> remote_header, local_header;
105         
106                 // stateData
107         int content_length, content_length_remaining;
108 };
109
110 class eHTTPD: public eServerSocket
111 {
112         friend class eHTTPConnection;
113         ePtrList<eHTTPPathResolver> resolver;
114         eMainloop *ml;
115 public:
116         eHTTPD(int port, eMainloop *ml);
117         void newConnection(int socket);
118
119         void addResolver(eHTTPPathResolver *r) { resolver.push_back(r); }
120         void removeResolver(eHTTPPathResolver *r) { resolver.remove(r); }
121 };
122
123 #endif