eb94d46297e8c34965d6168ea31f7eb3f0509a75
[enigma2.git] / lib / network / http_dyn.cpp
1 #include <lib/network/http_dyn.h>
2
3 eHTTPDyn::eHTTPDyn(eHTTPConnection *c, std::string result): eHTTPDataSource(c), result(result)
4 {
5         wptr=0;
6         char buffer[10];
7         snprintf(buffer, 10, "%d", size=result.length());
8         c->local_header["Content-Length"]=std::string(buffer);
9         if (c->code == -1)
10         {
11                 c->code=200;
12                 c->code_descr="OK";
13         }
14 }
15
16 eHTTPDyn::~eHTTPDyn()
17 {
18 }
19
20 int eHTTPDyn::doWrite(int hm)
21 {
22         int tw=size-wptr;
23         if (tw>hm)
24                 tw=hm;
25         if (tw<=0)
26                 return -1;
27         connection->writeBlock(result.c_str()+wptr, tw);
28         wptr+=tw;
29         return (size > wptr) ? 1 : -1;
30 }
31
32 eHTTPDynPathResolver::eHTTPDynPathResolver()
33 {
34 #warning autodelete removed
35 }
36
37 void eHTTPDynPathResolver::addDyn(std::string request, std::string path, std::string (*function)(std::string, std::string, std::string, eHTTPConnection*))
38 {
39         dyn.push_back(new eHTTPDynEntry(request, path, function));
40 }
41
42 eHTTPDataSource *eHTTPDynPathResolver::getDataSource(std::string request, std::string path, eHTTPConnection *conn)
43 {
44         std::string p, opt;
45         if (path.find('?')!=std::string::npos)
46         {
47                 p=path.substr(0, path.find('?'));
48                 opt=path.substr(path.find('?')+1);
49         }       else
50         {
51                 p=path;
52                 opt="";
53         }
54         for (ePtrList<eHTTPDynEntry>::iterator i(dyn); i != dyn.end(); ++i)
55                 if ((i->path==p) && (i->request==request))
56                 {
57                         conn->code=-1;
58                         std::string s=i->function(request, path, opt, conn);
59
60                         if (!s.empty())
61                                 return new eHTTPDyn(conn, s);
62
63                         return new eHTTPError(conn, 500);
64                 }
65         return 0;
66 }