add some more files to gitignore
[openblackhole/openblackhole-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         int errcode;
46 public:
47         eHTTPError(eHTTPConnection *c, int errcode);
48         ~eHTTPError() { }
49         void haveData();
50         int doWrite(int bytes);
51 };
52
53 class eHTTPConnection: public eSocket
54 {
55         void doError(int error);
56         
57         int getLine(std::string &line);
58         
59         int processLocalState();
60         int processRemoteState();
61         void writeString(const char *data);
62         
63         eHTTPDataSourcePtr data;
64         eHTTPD *parent;
65         
66         int buffersize;
67 private:
68         void readData();
69         void gotError(int);
70         void bytesWritten(int);
71         void hostConnected();
72         void destruct();
73 public:
74         Signal1<void,int> transferDone;
75         Signal1<eHTTPDataSource*,eHTTPConnection*> createDataSource;
76         enum
77         {
78                 /*
79                 
80                 < GET / HTTP/1.0
81                 < If-modified-since: bla
82                 <
83                 < Data
84                 > 200 OK HTTP/1.0
85                 > Content-Type: text/html
86                 >
87                 > Data
88                 */
89         
90                 stateWait, stateRequest, stateResponse, stateHeader, stateData, stateDone, stateClose
91         };
92         int localstate, remotestate;
93         int persistent;
94         
95         eHTTPConnection(int socket, int issocket, eHTTPD *parent, int persistent=0);
96         eHTTPConnection(eMainloop *ml); // ready to do "connectToHost"
97         static eHTTPConnection *doRequest(const char *uri, eMainloop *ml, int *error=0);
98         void start();
99         void gotHangup();
100         ~eHTTPConnection();
101         
102                 // stateRequest
103         std::string request, requestpath, httpversion;
104         int is09;
105         
106                 // stateResponse
107         
108         int code;
109         std::string code_descr;
110         
111         std::map<std::string,std::string> remote_header, local_header;
112         
113                 // stateData
114         int content_length, content_length_remaining;
115 };
116
117 class eHTTPD: public eServerSocket
118 {
119         friend class eHTTPConnection;
120         eSmartPtrList<iHTTPPathResolver> resolver;
121         eMainloop *ml;
122 public:
123         eHTTPD(int port, eMainloop *ml);
124         void newConnection(int socket);
125
126         void addResolver(iHTTPPathResolver *r) { resolver.push_back(r); }
127         void removeResolver(iHTTPPathResolver *r) { resolver.remove(r); }
128 };
129
130 #endif