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