103ac4bef59256d8587af039ee61b56fa0f50bbb
[swift-upb.git] / httpgw.cpp
1 #include "swift.h"
2
3 using namespace swift;
4
5 #define HTTPGW_MAX_CLIENT 128
6
7 enum {
8     HTTPGW_RANGE=0,
9     HTTPGW_MAX_HEADER=1
10 };
11 const char * HTTPGW_HEADERS[HTTPGW_MAX_HEADER] = {
12     "Content-Range"
13 };
14
15
16 struct http_gw_t {
17     int      id;
18     uint64_t offset;
19     uint64_t tosend;
20     int      transfer;
21     SOCKET   sink;
22     char*    headers[HTTPGW_MAX_HEADER];
23 } http_requests[HTTPGW_MAX_CLIENT];
24
25
26 int http_gw_reqs_open = 0;
27 int http_gw_reqs_count = 0;
28
29 void HttpGwNewRequestCallback (SOCKET http_conn);
30 void HttpGwNewRequestCallback (SOCKET http_conn);
31
32 http_gw_t* HttpGwFindRequest (SOCKET sock) {
33     for(int i=0; i<http_gw_reqs_open; i++)
34         if (http_requests[i].sink==sock)
35             return http_requests+i;
36     return NULL;
37 }
38
39
40 void HttpGwCloseConnection (SOCKET sock) {
41     http_gw_t* req = HttpGwFindRequest(sock);
42     if (req) {
43         dprintf("%s @%i closed http connection %i\n",tintstr(),req->id,sock);
44         for(int i=0; i<HTTPGW_MAX_HEADER; i++)
45             if (req->headers[i]) {
46                 free(req->headers[i]);
47                 req->headers[i] = NULL;
48             }
49         *req = http_requests[--http_gw_reqs_open];
50     }
51     swift::close_socket(sock);
52     swift::Datagram::Listen3rdPartySocket(sckrwecb_t(sock));
53 }
54
55
56 void HttpGwMayWriteCallback (SOCKET sink) {
57     http_gw_t* req = HttpGwFindRequest(sink);
58     uint64_t complete = swift::SeqComplete(req->transfer);
59     if (complete>req->offset) { // send data
60         char buf[1<<12];
61         uint64_t tosend = std::min((uint64_t)1<<12,complete-req->offset);
62         size_t rd = pread(req->transfer,buf,tosend,req->offset); // hope it is cached
63         if (rd<0) {
64             HttpGwCloseConnection(sink);
65             return;
66         }
67         int wn = send(sink, buf, rd, 0);
68         if (wn<0) {
69             print_error("send fails");
70             HttpGwCloseConnection(sink);
71             return;
72         }
73         dprintf("%s @%i sent %ib\n",tintstr(),req->id,(int)wn);
74         req->offset += wn;
75         req->tosend -= wn;
76     } else {
77         if (req->tosend==0) { // done; wait for new request
78             dprintf("%s @%i done\n",tintstr(),req->id);
79             sckrwecb_t wait_new_req(req->sink,HttpGwNewRequestCallback,NULL,HttpGwCloseConnection);
80             swift::Datagram::Listen3rdPartySocket (wait_new_req);
81         } else { // wait for data
82             dprintf("%s @%i waiting for data\n",tintstr(),req->id);
83             sckrwecb_t wait_swift_data(req->sink,NULL,NULL,HttpGwCloseConnection);
84             swift::Datagram::Listen3rdPartySocket(wait_swift_data);
85         }
86     }
87 }
88
89
90 void HttpGwSwiftProgressCallback (int transfer, bin64_t bin) {
91     for (int httpc=0; httpc<http_gw_reqs_open; httpc++)
92         if (http_requests[httpc].transfer==transfer)
93             if ( (bin.base_offset()<<10) == http_requests[httpc].offset ) {
94                 dprintf("%s @%i progress: %s\n",tintstr(),http_requests[httpc].id,bin.str());
95                 sckrwecb_t maywrite_callbacks
96                         (http_requests[httpc].sink,NULL,HttpGwMayWriteCallback,HttpGwCloseConnection);
97                 Datagram::Listen3rdPartySocket (maywrite_callbacks);
98             }
99 }
100
101
102 void HttpGwFirstProgressCallback (int transfer, bin64_t bin) {
103     if (bin!=bin64_t(0,0)) // need the first packet
104         return;
105     swift::RemoveProgressCallback(transfer,&HttpGwFirstProgressCallback);
106     swift::AddProgressCallback(transfer,&HttpGwSwiftProgressCallback);
107     for (int httpc=0; httpc<http_gw_reqs_open; httpc++) {
108         http_gw_t * req = http_requests + httpc;
109         if (req->transfer==transfer && req->tosend==0) { // FIXME states
110             uint64_t file_size = swift::Size(transfer);
111             char response[1024];
112             sprintf(response,
113                 "HTTP/1.1 200 OK\r\n"\
114                 "Connection: keep-alive\r\n"\
115                 "Content-Type: video/ogg\r\n"\
116                 "X-Content-Duration: 32\r\n"\
117                 "Content-Length: %lli\r\n"\
118                 "Accept-Ranges: bytes\r\n"\
119                 "\r\n",
120                 file_size);
121             send(req->sink,response,strlen(response),0);
122             req->tosend = file_size;
123             dprintf("%s @%i headers_sent size %lli\n",tintstr(),req->id,file_size);
124         }
125     }
126     HttpGwSwiftProgressCallback(transfer,bin);
127 }
128
129
130 void HttpGwNewRequestCallback (SOCKET http_conn){
131     http_gw_t* req = http_requests + http_gw_reqs_open++;
132     req->id = ++http_gw_reqs_count;
133     req->sink = http_conn;
134     req->offset = 0;
135     req->tosend = 0;
136     dprintf("%s @%i new http request\n",tintstr(),req->id);
137     // read headers - the thrilling part
138     // we surely do not support pipelining => one request at a time
139     #define HTTPGW_MAX_REQ_SIZE 1024
140     char buf[HTTPGW_MAX_REQ_SIZE+1];
141     int rd = recv(http_conn,buf,HTTPGW_MAX_REQ_SIZE,0);
142     if (rd<=0) { // if conn is closed by the peer, rd==0
143         HttpGwCloseConnection(http_conn);
144         return;
145     }
146     buf[rd] = 0;
147     // HTTP request line
148     char* reqline = strtok(buf,"\r\n");
149     char method[16], url[512], version[16], crlf[5];
150     if (3!=sscanf(reqline,"%16s %512s %16s",method,url,version)) {
151         HttpGwCloseConnection(http_conn);
152         return;
153     }
154     // HTTP header fields
155     char* headerline;
156     while (headerline=strtok(NULL,"\n\r")) {
157         char header[128], value[256];
158         if (2!=sscanf(headerline,"%120[^: ]: %250[^\r\n]",header,value)) {
159             HttpGwCloseConnection(http_conn);
160             return;
161         }
162         for(int i=0; i<HTTPGW_MAX_HEADER; i++)
163             if (0==strcasecmp(HTTPGW_HEADERS[i],header) && !req->headers[i])
164                 req->headers[i] = strdup(value);
165     }
166     // parse URL
167     char * hashch=strtok(url,"/"), hash[41];
168     while (hashch && (1!=sscanf(hashch,"%40[0123456789abcdefABCDEF]",hash) || strlen(hash)!=40))
169         hashch = strtok(NULL,"/");
170     if (strlen(hash)!=40) {
171         HttpGwCloseConnection(http_conn);
172         return;
173     }
174     dprintf("%s @%i demands %s\n",tintstr(),req->id,hash);
175     // initiate transmission
176     Sha1Hash root_hash = Sha1Hash(true,hash);
177     int file = swift::Find(root_hash);
178     if (file==-1)
179         file = swift::Open(hash,root_hash);
180     req->transfer = file;
181     if (swift::Size(file)) {
182         HttpGwFirstProgressCallback(file,bin64_t(0,0));
183     } else {
184         swift::AddProgressCallback(file,&HttpGwFirstProgressCallback);
185         sckrwecb_t install (http_conn,NULL,NULL,HttpGwCloseConnection);
186         swift::Datagram::Listen3rdPartySocket(install);
187     }
188 }
189
190
191 // be liberal in what you do, be conservative in what you accept
192 void HttpGwNewConnectionCallback (SOCKET serv) {
193     Address client_address;
194     socklen_t len;
195     SOCKET conn = accept (serv, (sockaddr*) & (client_address.addr), &len);
196     if (conn==INVALID_SOCKET) {
197         print_error("client conn fails");
198         return;
199     }
200     make_socket_nonblocking(conn);
201     // submit 3rd party socket to the swift loop
202     sckrwecb_t install
203         (conn,HttpGwNewRequestCallback,NULL,HttpGwCloseConnection);
204     swift::Datagram::Listen3rdPartySocket(install);
205 }
206
207
208 void HttpGwError (SOCKET s) {
209     print_error("httpgw is dead");
210     dprintf("%s @0 closed http gateway\n",tintstr());
211     close_socket(s);
212     swift::Datagram::Listen3rdPartySocket(sckrwecb_t(s));
213 }
214
215
216 #include <signal.h>
217 SOCKET InstallHTTPGateway (Address bind_to) {
218     SOCKET fd;
219     #define gw_ensure(x) { if (!(x)) { \
220     print_error("http binding fails"); close_socket(fd); \
221     return INVALID_SOCKET; } }
222     gw_ensure ( (fd=socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET );
223     int enable = true;
224     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (setsockoptptr_t)&enable, sizeof(int));
225     //setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (setsockoptptr_t)&enable, sizeof(int));
226     //struct sigaction act;
227     //memset(&act,0,sizeof(struct sigaction));
228     //act.sa_handler = SIG_IGN;
229     //sigaction (SIGPIPE, &act, NULL); // FIXME
230     signal( SIGPIPE, SIG_IGN );
231     gw_ensure ( 0==bind(fd, (sockaddr*)&(bind_to.addr), sizeof(struct sockaddr_in)) );
232     gw_ensure (make_socket_nonblocking(fd));
233     gw_ensure ( 0==listen(fd,8) );
234     sckrwecb_t install_http(fd,HttpGwNewConnectionCallback,NULL,HttpGwError);
235     gw_ensure (swift::Datagram::Listen3rdPartySocket(install_http));
236     dprintf("%s @0 installed http gateway on %s\n",tintstr(),bind_to.str());
237     return fd;
238 }