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