1 | #include <dim.h>
|
---|
2 |
|
---|
3 | #include <sys/types.h>
|
---|
4 | #include <sys/stat.h>
|
---|
5 | #include <fcntl.h>
|
---|
6 |
|
---|
7 | extern int WebDID_Debug;
|
---|
8 | extern int IsWebDID;
|
---|
9 |
|
---|
10 | #define BUFSIZE 8096
|
---|
11 | #define WERROR 42
|
---|
12 | #define SORRY 43
|
---|
13 | #define LOG 44
|
---|
14 |
|
---|
15 | #ifndef WIN32
|
---|
16 | #define O_BINARY 0
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | struct {
|
---|
20 | char *ext;
|
---|
21 | char *filetype;
|
---|
22 | } extensions [] = {
|
---|
23 | {"gif", "image/gif" },
|
---|
24 | {"jpg", "image/jpeg"},
|
---|
25 | {"jpeg","image/jpeg"},
|
---|
26 | {"png", "image/png" },
|
---|
27 | {"zip", "image/zip" },
|
---|
28 | {"gz", "image/gz" },
|
---|
29 | {"tar", "image/tar" },
|
---|
30 | {"htm", "text/html" },
|
---|
31 | {"html","text/html" },
|
---|
32 | {"js","text/javascript" },
|
---|
33 | {"css","text/css" },
|
---|
34 | {"php","text/php" },
|
---|
35 | {"json","application/json" },
|
---|
36 | {"ico","image/x-icon" },
|
---|
37 | {0,0} };
|
---|
38 |
|
---|
39 | extern int web_open_server();
|
---|
40 | extern void web_write();
|
---|
41 | extern int web_close();
|
---|
42 | extern int did_init();
|
---|
43 | extern int check_browser_changes();
|
---|
44 | extern int find_services();
|
---|
45 |
|
---|
46 | void getTime(char *buffer)
|
---|
47 | {
|
---|
48 | time_t nowtime;
|
---|
49 | struct tm *nowtm;
|
---|
50 |
|
---|
51 | nowtime = time((time_t *)0);
|
---|
52 | nowtm = (struct tm *)gmtime(&nowtime);
|
---|
53 | strftime(buffer, 128, "%a, %d %b %Y %H:%M:%S GMT", nowtm);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void log_it(int type, char *s1, char *s2, int conn_id)
|
---|
57 | {
|
---|
58 | char logbuffer[BUFSIZE*2];
|
---|
59 | static char date_buffer[128];
|
---|
60 | static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
61 |
|
---|
62 | switch (type) {
|
---|
63 | case WERROR: (void)printf("ERROR: %s:%s exiting pid=%d\n",s1, s2, getpid()); break;
|
---|
64 | case SORRY:
|
---|
65 | (void)sprintf(logbuffer, "<HTML><BODY><H1>webDid: %s %s</H1></BODY></HTML>\r\n", s1, s2);
|
---|
66 | dim_print_date_time();
|
---|
67 | (void)printf("webDid: %s %s\n",s1, s2);
|
---|
68 | getTime(date_buffer);
|
---|
69 | (void)sprintf(snd_buffer,"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
|
---|
70 | date_buffer, (int)strlen(logbuffer), "text/html");
|
---|
71 | (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
|
---|
72 | (void)web_write(conn_id,logbuffer,(int)strlen(logbuffer));
|
---|
73 | break;
|
---|
74 | case LOG: (void)printf("INFO: %s:%s:%d\n",s1, s2,conn_id);
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | /*
|
---|
78 | if(type == WERROR || type == SORRY)
|
---|
79 | {
|
---|
80 | sleep(60);
|
---|
81 | exit(3);
|
---|
82 | }
|
---|
83 | */
|
---|
84 | }
|
---|
85 |
|
---|
86 | int getParameters(char *buffer, char (*pars)[], char *ptrs[], int nmandatory)
|
---|
87 | {
|
---|
88 | char *ptr, *parptr;
|
---|
89 | int i, j, n = 0, found = 0;
|
---|
90 | int code;
|
---|
91 |
|
---|
92 | if(!strchr(buffer,'?'))
|
---|
93 | return 0;
|
---|
94 | parptr = (char *)pars;
|
---|
95 | for(i = 0; *parptr; i++)
|
---|
96 | {
|
---|
97 | n++;
|
---|
98 | ptrs[i] = 0;
|
---|
99 | if((ptr = strstr(buffer, parptr)))
|
---|
100 | {
|
---|
101 | ptrs[i] = ptr+(int)strlen(parptr);
|
---|
102 | found++;
|
---|
103 | }
|
---|
104 | parptr += 32;
|
---|
105 | }
|
---|
106 | ptrs[i] = 0;
|
---|
107 | for(i = 0; ptrs[i]; i++)
|
---|
108 | {
|
---|
109 | if((ptr = strchr(ptrs[i],'&')))
|
---|
110 | *ptr = '\0';
|
---|
111 | while((ptr = strchr(ptrs[i],'%')))
|
---|
112 | {
|
---|
113 | sscanf((ptr + 1),"%2X",&code);
|
---|
114 | sprintf(ptr,"%c",(char)code);
|
---|
115 | ptr++;
|
---|
116 | for(j = 0; *(ptr + 2); j++)
|
---|
117 | {
|
---|
118 | *ptr = *(ptr+2);
|
---|
119 | ptr++;
|
---|
120 | }
|
---|
121 | *ptr = '\0';
|
---|
122 | }
|
---|
123 | }
|
---|
124 | if(nmandatory == -1)
|
---|
125 | {
|
---|
126 | if(found == n)
|
---|
127 | return 1;
|
---|
128 | }
|
---|
129 | else if(found >= nmandatory)
|
---|
130 | {
|
---|
131 | return found;
|
---|
132 | }
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 | int getNodeParameters(char *buffer, char *node, int *browser)
|
---|
137 | {
|
---|
138 | char pars[4][32];
|
---|
139 | char *ptrs[4];
|
---|
140 | int ret;
|
---|
141 |
|
---|
142 | strcpy(pars[0],"node=");
|
---|
143 | strcpy(pars[1],"browser=");
|
---|
144 | pars[2][0] = '\0';
|
---|
145 | ret = getParameters(buffer, pars, ptrs, -1);
|
---|
146 | if(!ret)
|
---|
147 | return 0;
|
---|
148 | strcpy(node, ptrs[0]);
|
---|
149 | sscanf(ptrs[1],"%d",browser);
|
---|
150 | if(WebDID_Debug)
|
---|
151 | printf("parse pars - node %s, browser id %d\n", node, *browser);
|
---|
152 | return 1;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | int getServerParameters(char *buffer, char *node, char *server, int *pid, int *browser)
|
---|
157 | {
|
---|
158 | char pars[10][32];
|
---|
159 | char *ptrs[10];
|
---|
160 | int ret;
|
---|
161 |
|
---|
162 | strcpy(pars[0],"dimnode=");
|
---|
163 | strcpy(pars[1],"dimserver=");
|
---|
164 | strcpy(pars[2],"dimserverid=");
|
---|
165 | strcpy(pars[3],"browser=");
|
---|
166 | pars[4][0] = '\0';
|
---|
167 | ret = getParameters(buffer, pars, ptrs, -1);
|
---|
168 | if(!ret)
|
---|
169 | return 0;
|
---|
170 | strcpy(node, ptrs[0]);
|
---|
171 | strcpy(server, ptrs[1]);
|
---|
172 | sscanf(ptrs[2],"%d",pid);
|
---|
173 | sscanf(ptrs[3],"%d",browser);
|
---|
174 | if(WebDID_Debug)
|
---|
175 | printf("parse pars - node %s, server %s, pid %d, browser %d\n",node, server, *pid, *browser);
|
---|
176 | return 1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | int getServiceParameters(char *buffer, char *service, int *req, int* browser, int *force)
|
---|
180 | {
|
---|
181 | char pars[10][32];
|
---|
182 | char *ptrs[10];
|
---|
183 | int ret;
|
---|
184 |
|
---|
185 | strcpy(pars[0],"dimservice=");
|
---|
186 | strcpy(pars[1],"reqNr=");
|
---|
187 | strcpy(pars[2],"reqId=");
|
---|
188 | strcpy(pars[3],"force=");
|
---|
189 | pars[4][0] = '\0';
|
---|
190 | ret = getParameters(buffer, pars, ptrs, -1);
|
---|
191 | if(!ret)
|
---|
192 | return 0;
|
---|
193 | strcpy(service, ptrs[0]);
|
---|
194 | sscanf(ptrs[1],"%d",req);
|
---|
195 | sscanf(ptrs[2],"%d",browser);
|
---|
196 | sscanf(ptrs[3],"%d",force);
|
---|
197 | if(WebDID_Debug)
|
---|
198 | printf("\nparse service pars - service %s %d %d %d\n\n",service, *req, *browser, *force);
|
---|
199 | return 1;
|
---|
200 | }
|
---|
201 |
|
---|
202 | int getServiceParametersDim(char *buffer, char *service, void *nolink, int *nolinksize, int *update)
|
---|
203 | {
|
---|
204 | char pars[10][32];
|
---|
205 | char *ptrs[10];
|
---|
206 | int ret;
|
---|
207 | char nolinkstr[MAX_NAME] = { '\0' }, updatestr[10] = { '\0' };
|
---|
208 | char servicestr[MAX_NAME];
|
---|
209 |
|
---|
210 | strcpy(pars[0], "service=");
|
---|
211 | strcpy(pars[1], "nolink=");
|
---|
212 | strcpy(pars[2], "update=");
|
---|
213 | pars[3][0] = '\0';
|
---|
214 | ret = getParameters(buffer, pars, ptrs, 1);
|
---|
215 | if (!ret)
|
---|
216 | return 0;
|
---|
217 | strcpy(servicestr, ptrs[0]);
|
---|
218 | if (ptrs[1])
|
---|
219 | strcpy(nolinkstr, ptrs[1]);
|
---|
220 | if (ptrs[2])
|
---|
221 | strcpy(updatestr, ptrs[2]);
|
---|
222 | // sscanf(ptrs[1], "%d", req);
|
---|
223 | // sscanf(ptrs[2], "%d", browser);
|
---|
224 | // sscanf(ptrs[3], "%d", force);
|
---|
225 | if (servicestr[0] == '"')
|
---|
226 | {
|
---|
227 | strcpy((char *)service, &servicestr[1]);
|
---|
228 | ((char *)service)[strlen(servicestr) - 2] = '\0';
|
---|
229 | }
|
---|
230 | else
|
---|
231 | {
|
---|
232 | strcpy(service, ptrs[0]);
|
---|
233 | }
|
---|
234 | if ((nolink) && (nolinksize))
|
---|
235 | {
|
---|
236 | if(nolinkstr[0] != '\0')
|
---|
237 | {
|
---|
238 | if (nolinkstr[0] == '"')
|
---|
239 | {
|
---|
240 | strcpy((char *)nolink, &nolinkstr[1]);
|
---|
241 | ((char *)nolink)[strlen(nolinkstr) - 2] = '\0';
|
---|
242 | *nolinksize = strlen(nolinkstr) - 2 + 1;
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | sscanf(nolinkstr, "%d", (int *)nolink);
|
---|
247 | *nolinksize = sizeof(int);
|
---|
248 | }
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | *((int *)nolink) = -1;
|
---|
253 | *nolinksize = sizeof(int);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | if (update)
|
---|
257 | {
|
---|
258 | if (updatestr[0] != '\0')
|
---|
259 | {
|
---|
260 | sscanf(updatestr, "%d", (int *)update);
|
---|
261 | }
|
---|
262 | }
|
---|
263 | if (WebDID_Debug)
|
---|
264 | printf("\nparse service pars - service %s %s %s\n\n", service, nolinkstr, updatestr);
|
---|
265 | return 1;
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | extern char JSONHeader[];
|
---|
270 | extern char *JSONBuffer;
|
---|
271 |
|
---|
272 | static char *conv_buffer = 0;
|
---|
273 | static int conv_buffer_size = 0;
|
---|
274 |
|
---|
275 | char *unescape(char *buffer)
|
---|
276 | {
|
---|
277 | int buffer_size;
|
---|
278 | char *ptr, *ptr1;
|
---|
279 | int code;
|
---|
280 |
|
---|
281 | buffer_size = (int)strlen(buffer) + 1;
|
---|
282 | if(buffer_size > conv_buffer_size )
|
---|
283 | {
|
---|
284 | if(conv_buffer_size)
|
---|
285 | free(conv_buffer);
|
---|
286 | conv_buffer = malloc((size_t)buffer_size);
|
---|
287 | conv_buffer_size = buffer_size;
|
---|
288 | }
|
---|
289 | ptr = buffer;
|
---|
290 | ptr1 = conv_buffer;
|
---|
291 | while(*ptr)
|
---|
292 | {
|
---|
293 | if(*ptr != '%')
|
---|
294 | {
|
---|
295 | *ptr1 = *ptr;
|
---|
296 | ptr++;
|
---|
297 | ptr1++;
|
---|
298 | }
|
---|
299 | else
|
---|
300 | {
|
---|
301 | ptr++;
|
---|
302 | sscanf(ptr,"%2X",&code);
|
---|
303 | sprintf(ptr1,"%c",code);
|
---|
304 | ptr += 2;
|
---|
305 | ptr1++;
|
---|
306 | *ptr1 = '\0';
|
---|
307 | }
|
---|
308 | }
|
---|
309 | *ptr1 = '\0';
|
---|
310 | return conv_buffer;
|
---|
311 | }
|
---|
312 |
|
---|
313 | void sendData(int conn_id, char *buffer, int type, int oper)
|
---|
314 | {
|
---|
315 | static char date_buffer[128];
|
---|
316 | static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
317 | static char snd_data_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
318 | char *ptr = 0;
|
---|
319 | char node[128], server[256], service[256];
|
---|
320 | int pid, ret, req, browser, force;
|
---|
321 | extern char *update_services();
|
---|
322 | extern char *update_service_data();
|
---|
323 | extern char *getJSONHeader();
|
---|
324 | extern char *getJSONBuffer();
|
---|
325 | extern char *getJSONDimBuffer();
|
---|
326 | char datatype[128];
|
---|
327 | char *conv_buffer;
|
---|
328 | int conv_size = 0;
|
---|
329 | char nolink[MAX_NAME];
|
---|
330 | int nolinksize;
|
---|
331 | int update;
|
---|
332 |
|
---|
333 | conv_buffer = buffer;
|
---|
334 | strcpy(datatype,"application/json");
|
---|
335 | if(type == 0)
|
---|
336 | {
|
---|
337 | ptr = getJSONHeader(0);
|
---|
338 | }
|
---|
339 | else if(type == 1)
|
---|
340 | {
|
---|
341 | ret = getNodeParameters(conv_buffer, node, &browser);
|
---|
342 | ptr = getJSONBuffer(node, browser);
|
---|
343 | }
|
---|
344 | else if(type == 2)
|
---|
345 | {
|
---|
346 | ret = getServerParameters(conv_buffer, node, server, &pid, &browser);
|
---|
347 | if(!ret)
|
---|
348 | {
|
---|
349 | strcpy(snd_data_buffer,"{}");
|
---|
350 | ptr = snd_data_buffer;
|
---|
351 | }
|
---|
352 | else
|
---|
353 | {
|
---|
354 | ptr = update_services(node, server, pid, browser);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | else if(type == 3)
|
---|
358 | {
|
---|
359 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
360 | if(!ret)
|
---|
361 | {
|
---|
362 | strcpy(snd_data_buffer,"{}");
|
---|
363 | ptr = snd_data_buffer;
|
---|
364 | }
|
---|
365 | else
|
---|
366 | {
|
---|
367 | update_service_data(service, conn_id, 0, req, browser, force, 1, 0, 0);
|
---|
368 | return;
|
---|
369 | }
|
---|
370 | }
|
---|
371 | else if(type == 4)
|
---|
372 | {
|
---|
373 | ptr = conv_buffer;
|
---|
374 | if(WebDID_Debug)
|
---|
375 | printf("%s\n",ptr);
|
---|
376 | strcpy(datatype,"text/html");
|
---|
377 | }
|
---|
378 | else if(type == 10)
|
---|
379 | {
|
---|
380 | ret = getServiceParametersDim(conv_buffer, service, nolink, &nolinksize, &update);
|
---|
381 | if(!ret)
|
---|
382 | {
|
---|
383 | strcpy(snd_data_buffer,"{}");
|
---|
384 | ptr = snd_data_buffer;
|
---|
385 | }
|
---|
386 | else
|
---|
387 | {
|
---|
388 | if (oper == 0)
|
---|
389 | update = -1;
|
---|
390 | else if (oper == -1)
|
---|
391 | update = -2;
|
---|
392 | update_service_data(service, conn_id, update, 0, conn_id, 1, 0, nolink, nolinksize);
|
---|
393 | return;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | else if(type == 11)
|
---|
397 | {
|
---|
398 | ptr = conv_buffer;
|
---|
399 | if(WebDID_Debug)
|
---|
400 | printf("%s\n",ptr);
|
---|
401 | // strcpy(datatype,"application/octet-stream");
|
---|
402 | strcpy(datatype,"text/html");
|
---|
403 | }
|
---|
404 | else if(type == 5)
|
---|
405 | {
|
---|
406 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
407 | if(!ret)
|
---|
408 | {
|
---|
409 | strcpy(snd_data_buffer,"{}");
|
---|
410 | ptr = snd_data_buffer;
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | check_browser_changes(service, conn_id, 0, req, browser, force);
|
---|
415 | return;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | else if(type == 6)
|
---|
419 | {
|
---|
420 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
421 | if(!ret)
|
---|
422 | {
|
---|
423 | strcpy(snd_data_buffer,"{}");
|
---|
424 | ptr = snd_data_buffer;
|
---|
425 | }
|
---|
426 | else
|
---|
427 | {
|
---|
428 | find_services(service, conn_id, browser, force);
|
---|
429 | if(force == -1)
|
---|
430 | strcpy(snd_data_buffer,"");
|
---|
431 | else
|
---|
432 | strcpy(snd_data_buffer,"load");
|
---|
433 | ptr = snd_data_buffer;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | else if (type == 12)
|
---|
437 | {
|
---|
438 | ret = getServiceParametersDim(conv_buffer, service, 0, 0, 0);
|
---|
439 | if (!ret)
|
---|
440 | {
|
---|
441 | strcpy(snd_data_buffer, "{}");
|
---|
442 | ptr = snd_data_buffer;
|
---|
443 | }
|
---|
444 | else
|
---|
445 | {
|
---|
446 | find_services(service, conn_id, conn_id, 1);
|
---|
447 | ptr = getJSONDimBuffer("src", conn_id);
|
---|
448 | }
|
---|
449 | }
|
---|
450 | if (IsWebDID)
|
---|
451 | {
|
---|
452 | getTime(date_buffer);
|
---|
453 | /*
|
---|
454 | (void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nKeep-Alive: timeout=1000\r\nConnection: keep-alive\r\n\r\n",
|
---|
455 | date_buffer, (int)strlen(ptr), datatype);
|
---|
456 | */
|
---|
457 | (void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
|
---|
458 | date_buffer, (int)strlen(ptr), datatype);
|
---|
459 | (void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer));
|
---|
460 | }
|
---|
461 | if(WebDID_Debug)
|
---|
462 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
463 | (void)web_write(conn_id,ptr,(int)strlen(ptr));
|
---|
464 | if(WebDID_Debug == 2)
|
---|
465 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 | void sendSmiData(int conn_id, char *buffer, int type)
|
---|
470 | {
|
---|
471 | static char date_buffer[128];
|
---|
472 | static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
473 | static char snd_data_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
474 | char *ptr = 0;
|
---|
475 | char node[128], server[256], service[256];
|
---|
476 | int pid, ret, req, browser, force;
|
---|
477 | extern char *update_services(), *update_smi_objects();
|
---|
478 | extern char *update_service_data();
|
---|
479 | extern char *getJSONHeader();
|
---|
480 | extern char *getJSONBuffer(), *getJSONSmiBuffer();
|
---|
481 | char datatype[128];
|
---|
482 | char *conv_buffer;
|
---|
483 |
|
---|
484 | conv_buffer = buffer;
|
---|
485 | strcpy(datatype,"application/json");
|
---|
486 | if(type == 0)
|
---|
487 | {
|
---|
488 | ptr = getJSONHeader(1);
|
---|
489 | }
|
---|
490 | else if(type == 1)
|
---|
491 | {
|
---|
492 | ret = getNodeParameters(conv_buffer, node, &browser);
|
---|
493 | ptr = getJSONSmiBuffer(node, browser);
|
---|
494 | }
|
---|
495 | else if(type == 2)
|
---|
496 | {
|
---|
497 | ret = getServerParameters(conv_buffer, node, server, &pid, &browser);
|
---|
498 | if(!ret)
|
---|
499 | {
|
---|
500 | strcpy(snd_data_buffer,"{}");
|
---|
501 | ptr = snd_data_buffer;
|
---|
502 | }
|
---|
503 | else
|
---|
504 | {
|
---|
505 | ptr = update_smi_objects(node, server, pid, browser);
|
---|
506 | }
|
---|
507 | }
|
---|
508 | else if(type == 3)
|
---|
509 | {
|
---|
510 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
511 | if(!ret)
|
---|
512 | {
|
---|
513 | strcpy(snd_data_buffer,"{}");
|
---|
514 | ptr = snd_data_buffer;
|
---|
515 | }
|
---|
516 | else
|
---|
517 | {
|
---|
518 | update_service_data(service, conn_id, 0, req, browser, force, 0, 0);
|
---|
519 | return;
|
---|
520 | }
|
---|
521 | }
|
---|
522 | else if(type == 4)
|
---|
523 | {
|
---|
524 | ptr = conv_buffer;
|
---|
525 | if(WebDID_Debug)
|
---|
526 | printf("%s\n",ptr);
|
---|
527 | strcpy(datatype,"text/html");
|
---|
528 | }
|
---|
529 | else if(type == 5)
|
---|
530 | {
|
---|
531 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
532 | if(!ret)
|
---|
533 | {
|
---|
534 | strcpy(snd_data_buffer,"{}");
|
---|
535 | ptr = snd_data_buffer;
|
---|
536 | }
|
---|
537 | else
|
---|
538 | {
|
---|
539 | check_browser_changes(service, conn_id, 0, req, browser, force);
|
---|
540 | return;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | else if(type == 6)
|
---|
544 | {
|
---|
545 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
546 | if(!ret)
|
---|
547 | {
|
---|
548 | strcpy(snd_data_buffer,"{}");
|
---|
549 | ptr = snd_data_buffer;
|
---|
550 | }
|
---|
551 | else
|
---|
552 | {
|
---|
553 | find_services(service, conn_id, browser, force);
|
---|
554 | if(force == -1)
|
---|
555 | strcpy(snd_data_buffer,"");
|
---|
556 | else
|
---|
557 | strcpy(snd_data_buffer,"load");
|
---|
558 | ptr = snd_data_buffer;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | if (IsWebDID)
|
---|
562 | {
|
---|
563 | getTime(date_buffer);
|
---|
564 | (void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
|
---|
565 | date_buffer, (int)strlen(ptr), datatype);
|
---|
566 | (void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer));
|
---|
567 | }
|
---|
568 | if(WebDID_Debug)
|
---|
569 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
570 | (void)web_write(conn_id,ptr,(int)strlen(ptr));
|
---|
571 | if(WebDID_Debug == 2)
|
---|
572 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
|
---|
573 | }
|
---|
574 |
|
---|
575 | void sendFile(int conn_id, char *buffer, int size)
|
---|
576 | {
|
---|
577 | int j, file_fd, buflen, len;
|
---|
578 | int i, ret;
|
---|
579 | char * fstr;
|
---|
580 | int flen;
|
---|
581 | static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
582 | static char date_buffer[128];
|
---|
583 | char *ptr;
|
---|
584 | int operation = 0;
|
---|
585 |
|
---|
586 |
|
---|
587 | ret = size;
|
---|
588 | if(ret > 0 && ret < BUFSIZE) /* return code is valid chars */
|
---|
589 | buffer[ret]=0; /* terminate the buffer */
|
---|
590 | else buffer[0]=0;
|
---|
591 |
|
---|
592 | if(WebDID_Debug)
|
---|
593 | printf("Got %s\n", buffer);
|
---|
594 | if (IsWebDID)
|
---|
595 | {
|
---|
596 | if (strncmp(buffer, "GET ", 4) && strncmp(buffer, "get ", 4))
|
---|
597 | {
|
---|
598 | log_it(SORRY, "Only simple GET operation supported", buffer, conn_id);
|
---|
599 | return;
|
---|
600 | }
|
---|
601 | }
|
---|
602 | for(i=4;i<BUFSIZE;i++)
|
---|
603 | { /* null terminate after the second space to ignore extra stuff */
|
---|
604 | if(buffer[i] == ' ')
|
---|
605 | { /* string is "GET URL " +lots of other stuff */
|
---|
606 | buffer[i] = 0;
|
---|
607 | break;
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | if(WebDID_Debug)
|
---|
612 | printf("Got 1 %s\n", buffer);
|
---|
613 | for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */
|
---|
614 | {
|
---|
615 | if(buffer[j] == '.' && buffer[j+1] == '.')
|
---|
616 | {
|
---|
617 | log_it(SORRY,"Parent directory (..) path names not supported",buffer,conn_id);
|
---|
618 | return;
|
---|
619 | }
|
---|
620 | }
|
---|
621 | if((int)strlen(buffer) == 5)
|
---|
622 | {
|
---|
623 | if( !strncmp(&buffer[0],"GET /",5) || !strncmp(&buffer[0],"get /",5) )
|
---|
624 | /* convert no filename to index file */
|
---|
625 | (void)strcpy(buffer,"GET /index.html");
|
---|
626 | }
|
---|
627 | if((int)strlen(buffer) == 8)
|
---|
628 | {
|
---|
629 | if( !strncmp(&buffer[0],"GET /smi",8) || !strncmp(&buffer[0],"get /smi",8) )
|
---|
630 | /* convert no filename to index file */
|
---|
631 | (void)strcpy(buffer,"GET /smi/index.html");
|
---|
632 | }
|
---|
633 | /* work out the file type and check we support it */
|
---|
634 | buflen=(int)strlen(buffer);
|
---|
635 | fstr = (char *)0;
|
---|
636 | for(i=0;extensions[i].ext != 0;i++)
|
---|
637 | {
|
---|
638 | len = (int)strlen(extensions[i].ext);
|
---|
639 | if( !strncmp(&buffer[buflen-len], extensions[i].ext, (size_t)len))
|
---|
640 | {
|
---|
641 | fstr =extensions[i].filetype;
|
---|
642 | break;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | /*
|
---|
646 | (void)sprintf(snd_buffer,"HTTP/1.1 100 Continue\r\n\r\n");
|
---|
647 | (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
|
---|
648 | printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
649 | */
|
---|
650 | ptr = &buffer[5];
|
---|
651 | if (!IsWebDID)
|
---|
652 | {
|
---|
653 | if (!strncmp(&buffer[0], "GET", 3) || !strncmp(&buffer[0], "get", 3))
|
---|
654 | {
|
---|
655 | operation = 0;
|
---|
656 | }
|
---|
657 | if (!strncmp(&buffer[0], "SUBSCRIBE", 9) || !strncmp(&buffer[0], "subscribe", 9))
|
---|
658 | {
|
---|
659 | operation = 1;
|
---|
660 | ptr = &buffer[11];
|
---|
661 | }
|
---|
662 | if (!strncmp(&buffer[0], "UNSUBSCRIBE", 11) || !strncmp(&buffer[0], "unsubscribe", 11))
|
---|
663 | {
|
---|
664 | operation = -1;
|
---|
665 | ptr = &buffer[13];
|
---|
666 | }
|
---|
667 | }
|
---|
668 | if(fstr == 0)
|
---|
669 | {
|
---|
670 | if(WebDID_Debug)
|
---|
671 | printf("Got %s\n", buffer);
|
---|
672 | if (!strncmp(ptr, "didHeader", 9))
|
---|
673 | {
|
---|
674 | sendData(conn_id, ptr, 0, 0);
|
---|
675 | return;
|
---|
676 | }
|
---|
677 | else if (!strncmp(ptr, "didData", 7))
|
---|
678 | {
|
---|
679 | sendData(conn_id, ptr, 1, 0);
|
---|
680 | return;
|
---|
681 | }
|
---|
682 | else if (!strncmp(ptr, "didServices", 11))
|
---|
683 | {
|
---|
684 | sendData(conn_id, ptr, 2, 0);
|
---|
685 | return;
|
---|
686 | }
|
---|
687 | else if (!strncmp(ptr, "didServiceData", 14))
|
---|
688 | {
|
---|
689 | sendData(conn_id, ptr, 3, 0);
|
---|
690 | return;
|
---|
691 | }
|
---|
692 | else if (!strncmp(ptr, "dimService", 10))
|
---|
693 | {
|
---|
694 | sendData(conn_id, ptr, 10, operation);
|
---|
695 | return;
|
---|
696 | }
|
---|
697 | else if (!strncmp(ptr, "didPoll", 7))
|
---|
698 | {
|
---|
699 | sendData(conn_id, ptr, 5, 0);
|
---|
700 | return;
|
---|
701 | }
|
---|
702 | else if (!strncmp(ptr, "didQuery", 8))
|
---|
703 | {
|
---|
704 | sendData(conn_id, ptr, 6, 0);
|
---|
705 | return;
|
---|
706 | }
|
---|
707 | else if (!strncmp(ptr, "dimBrowser", 10))
|
---|
708 | {
|
---|
709 | sendData(conn_id, ptr, 12, 0);
|
---|
710 | return;
|
---|
711 | }
|
---|
712 | else if (!strncmp(ptr, "smiData", 7))
|
---|
713 | {
|
---|
714 | sendSmiData(conn_id, ptr, 1);
|
---|
715 | return;
|
---|
716 | }
|
---|
717 | else if (!strncmp(ptr, "smiObjects", 10))
|
---|
718 | {
|
---|
719 | sendSmiData(conn_id, ptr, 2);
|
---|
720 | return;
|
---|
721 | }
|
---|
722 | /*
|
---|
723 | if((!strncmp(&buffer[5],"didData",7)) || (!strncmp(&buffer[5],"didServices",11)))
|
---|
724 | {
|
---|
725 | if(ptr = strchr(&buffer[5],'/'))
|
---|
726 | {
|
---|
727 | *ptr = '\0';
|
---|
728 | }
|
---|
729 | buflen=(int)strlen(buffer);
|
---|
730 | for(i=0;extensions[i].ext != 0;i++)
|
---|
731 | {
|
---|
732 | len = (int)strlen(extensions[i].ext);
|
---|
733 | if( !strncmp(&buffer[buflen-len], extensions[i].ext, len))
|
---|
734 | {
|
---|
735 | fstr =extensions[i].filetype;
|
---|
736 | printf("fstr %s", fstr);
|
---|
737 | break;
|
---|
738 | }
|
---|
739 | }
|
---|
740 | if(!strncmp(&buffer[5],"didData",7))
|
---|
741 | {
|
---|
742 | sendData(conn_id, &buffer[5]);
|
---|
743 | }
|
---|
744 | else if(!strncmp(&buffer[5],"didServices",11))
|
---|
745 | {
|
---|
746 | sendData(conn_id, &buffer[5]);
|
---|
747 | }
|
---|
748 | }
|
---|
749 | */
|
---|
750 | else
|
---|
751 | {
|
---|
752 | log_it(SORRY,"file extension type not supported",buffer,conn_id);
|
---|
753 | return;
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
|
---|
758 | {
|
---|
759 | log_it(SORRY, "failed to open file",&buffer[5],conn_id);
|
---|
760 | return;
|
---|
761 | }
|
---|
762 |
|
---|
763 | flen = 0;
|
---|
764 | while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 )
|
---|
765 | {
|
---|
766 | flen += ret;
|
---|
767 | }
|
---|
768 | close(file_fd);
|
---|
769 |
|
---|
770 | if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
|
---|
771 | {
|
---|
772 | log_it(SORRY, "failed to open file",&buffer[5],conn_id);
|
---|
773 | return;
|
---|
774 | }
|
---|
775 |
|
---|
776 | getTime(date_buffer);
|
---|
777 | (void)sprintf(snd_buffer,"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
|
---|
778 | date_buffer, flen, fstr);
|
---|
779 | (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
|
---|
780 | if(WebDID_Debug)
|
---|
781 | printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
782 |
|
---|
783 | /* send file in 8KB block - last block may be smaller */
|
---|
784 | while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 ) {
|
---|
785 | (void)web_write(conn_id,snd_buffer,ret);
|
---|
786 | if(WebDID_Debug == 2)
|
---|
787 | printf("SENDING data to conn %d: %d bytes\n",conn_id, ret);
|
---|
788 | }
|
---|
789 | close(file_fd);
|
---|
790 | #ifdef LINUX
|
---|
791 | sleep(1); /* to allow socket to drain */
|
---|
792 | #endif
|
---|
793 | }
|
---|
794 |
|
---|
795 | static void handler( int conn_id, char *packet, int size, int status )
|
---|
796 | {
|
---|
797 | int close_browser();
|
---|
798 |
|
---|
799 | switch(status)
|
---|
800 | {
|
---|
801 | case STA_DISC: /* connection broken */
|
---|
802 | if(WebDID_Debug)
|
---|
803 | {
|
---|
804 | dim_print_date_time();
|
---|
805 | printf(" Disconnect received - conn: %d to %s@%s\n", conn_id,
|
---|
806 | Net_conns[conn_id].task,Net_conns[conn_id].node );
|
---|
807 | }
|
---|
808 | close_browser(conn_id);
|
---|
809 | web_close(conn_id);
|
---|
810 | break;
|
---|
811 | case STA_CONN: /* connection received */
|
---|
812 | if(WebDID_Debug)
|
---|
813 | {
|
---|
814 | dim_print_date_time();
|
---|
815 | printf(" Connection request received - conn: %d\n", conn_id);
|
---|
816 | }
|
---|
817 | break;
|
---|
818 | case STA_DATA: /* normal packet */
|
---|
819 | /*
|
---|
820 | dim_print_date_time();
|
---|
821 | printf(" conn %d packet received:\n", conn_id);
|
---|
822 | printf("packet size = %d\n", size);
|
---|
823 | printf("%s\n",packet);
|
---|
824 | fflush(stdout);
|
---|
825 | */
|
---|
826 | sendFile(conn_id, packet, size);
|
---|
827 | break;
|
---|
828 | default:
|
---|
829 | dim_print_date_time();
|
---|
830 | printf( " - DIM panic: recv_rout(): Bad switch, exiting...\n");
|
---|
831 | abort();
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | static void error_handler(int conn_id, int severity, int errcode, char *reason)
|
---|
836 | {
|
---|
837 | if(conn_id){}
|
---|
838 | if(errcode){}
|
---|
839 | dim_print_msg(reason, severity);
|
---|
840 | /*
|
---|
841 | if(severity == 3)
|
---|
842 | {
|
---|
843 | printf("Exiting!\n");
|
---|
844 | exit(2);
|
---|
845 | }
|
---|
846 | */
|
---|
847 | }
|
---|
848 |
|
---|
849 | int main(int argc, char **argv)
|
---|
850 | {
|
---|
851 | int port;
|
---|
852 | int proto;
|
---|
853 | char dns_node[128];
|
---|
854 | int web_get_port();
|
---|
855 | char *ptr;
|
---|
856 | char currwd[256];
|
---|
857 |
|
---|
858 | if(argc){}
|
---|
859 |
|
---|
860 | strcpy(currwd, argv[0]);
|
---|
861 | printf("arg %s\n",currwd);
|
---|
862 | ptr = strrchr(currwd,'/');
|
---|
863 | if(ptr)
|
---|
864 | {
|
---|
865 | *ptr = '\0';
|
---|
866 | }
|
---|
867 | ptr = strrchr(currwd,'\\');
|
---|
868 | if(ptr)
|
---|
869 | {
|
---|
870 | *ptr = '\0';
|
---|
871 | }
|
---|
872 | // chdir(currwd);
|
---|
873 | log_it(LOG,"webDim starting",argv[1],getpid());
|
---|
874 | /* setup the network socket */
|
---|
875 | proto = 1;
|
---|
876 | port = web_get_port();
|
---|
877 | get_node_name(dns_node);
|
---|
878 | did_init(dns_node, DNS_PORT);
|
---|
879 | if(IsWebDID)
|
---|
880 | {
|
---|
881 | if (!web_open_server("DID", handler, &proto, &port, error_handler))
|
---|
882 | return(0);
|
---|
883 | }
|
---|
884 | else
|
---|
885 | {
|
---|
886 | if (!web_open_server("DimClient", handler, &proto, &port, error_handler))
|
---|
887 | return(0);
|
---|
888 | }
|
---|
889 | /*
|
---|
890 | ret = matchString("hello world","*ll*");
|
---|
891 | printf("%s %s %d\n", "hello world","*ll*",ret);
|
---|
892 | ret = matchString("hello world","ll*");
|
---|
893 | printf("%s %s %d\n", "hello world","ll*",ret);
|
---|
894 | */
|
---|
895 | while(1)
|
---|
896 | sleep(10);
|
---|
897 | return(0);
|
---|
898 | }
|
---|