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 | char nolink[MAX_NAME];
|
---|
329 | int nolinksize;
|
---|
330 | int update;
|
---|
331 |
|
---|
332 | conv_buffer = buffer;
|
---|
333 | strcpy(datatype,"application/json");
|
---|
334 | if(type == 0)
|
---|
335 | {
|
---|
336 | ptr = getJSONHeader(0);
|
---|
337 | }
|
---|
338 | else if(type == 1)
|
---|
339 | {
|
---|
340 | ret = getNodeParameters(conv_buffer, node, &browser);
|
---|
341 | ptr = getJSONBuffer(node, browser);
|
---|
342 | }
|
---|
343 | else if(type == 2)
|
---|
344 | {
|
---|
345 | ret = getServerParameters(conv_buffer, node, server, &pid, &browser);
|
---|
346 | if(!ret)
|
---|
347 | {
|
---|
348 | strcpy(snd_data_buffer,"{}");
|
---|
349 | ptr = snd_data_buffer;
|
---|
350 | }
|
---|
351 | else
|
---|
352 | {
|
---|
353 | ptr = update_services(node, server, pid, browser);
|
---|
354 | }
|
---|
355 | }
|
---|
356 | else if(type == 3)
|
---|
357 | {
|
---|
358 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
359 | if(!ret)
|
---|
360 | {
|
---|
361 | strcpy(snd_data_buffer,"{}");
|
---|
362 | ptr = snd_data_buffer;
|
---|
363 | }
|
---|
364 | else
|
---|
365 | {
|
---|
366 | update_service_data(service, conn_id, 0, req, browser, force, 1, 0, 0);
|
---|
367 | return;
|
---|
368 | }
|
---|
369 | }
|
---|
370 | else if(type == 4)
|
---|
371 | {
|
---|
372 | ptr = conv_buffer;
|
---|
373 | if(WebDID_Debug)
|
---|
374 | printf("%s\n",ptr);
|
---|
375 | strcpy(datatype,"text/html");
|
---|
376 | }
|
---|
377 | else if(type == 10)
|
---|
378 | {
|
---|
379 | ret = getServiceParametersDim(conv_buffer, service, nolink, &nolinksize, &update);
|
---|
380 | if(!ret)
|
---|
381 | {
|
---|
382 | strcpy(snd_data_buffer,"{}");
|
---|
383 | ptr = snd_data_buffer;
|
---|
384 | }
|
---|
385 | else
|
---|
386 | {
|
---|
387 | if (oper == 0)
|
---|
388 | update = -1;
|
---|
389 | else if (oper == -1)
|
---|
390 | update = -2;
|
---|
391 | update_service_data(service, conn_id, update, 0, conn_id, 1, 0, nolink, nolinksize);
|
---|
392 | return;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | else if(type == 11)
|
---|
396 | {
|
---|
397 | ptr = conv_buffer;
|
---|
398 | if(WebDID_Debug)
|
---|
399 | printf("%s\n",ptr);
|
---|
400 | // strcpy(datatype,"application/octet-stream");
|
---|
401 | strcpy(datatype,"text/html");
|
---|
402 | }
|
---|
403 | else if(type == 5)
|
---|
404 | {
|
---|
405 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
406 | if(!ret)
|
---|
407 | {
|
---|
408 | strcpy(snd_data_buffer,"{}");
|
---|
409 | ptr = snd_data_buffer;
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {
|
---|
413 | check_browser_changes(service, conn_id, 0, req, browser, force);
|
---|
414 | return;
|
---|
415 | }
|
---|
416 | }
|
---|
417 | else if(type == 6)
|
---|
418 | {
|
---|
419 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
420 | if(!ret)
|
---|
421 | {
|
---|
422 | strcpy(snd_data_buffer,"{}");
|
---|
423 | ptr = snd_data_buffer;
|
---|
424 | }
|
---|
425 | else
|
---|
426 | {
|
---|
427 | find_services(service, conn_id, browser, force);
|
---|
428 | if(force == -1)
|
---|
429 | strcpy(snd_data_buffer,"");
|
---|
430 | else
|
---|
431 | strcpy(snd_data_buffer,"load");
|
---|
432 | ptr = snd_data_buffer;
|
---|
433 | }
|
---|
434 | }
|
---|
435 | else if (type == 12)
|
---|
436 | {
|
---|
437 | ret = getServiceParametersDim(conv_buffer, service, 0, 0, 0);
|
---|
438 | if (!ret)
|
---|
439 | {
|
---|
440 | strcpy(snd_data_buffer, "{}");
|
---|
441 | ptr = snd_data_buffer;
|
---|
442 | }
|
---|
443 | else
|
---|
444 | {
|
---|
445 | find_services(service, conn_id, conn_id, 1);
|
---|
446 | ptr = getJSONDimBuffer("src", conn_id);
|
---|
447 | }
|
---|
448 | }
|
---|
449 | if (IsWebDID)
|
---|
450 | {
|
---|
451 | getTime(date_buffer);
|
---|
452 | /*
|
---|
453 | (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",
|
---|
454 | date_buffer, (int)strlen(ptr), datatype);
|
---|
455 | */
|
---|
456 | (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",
|
---|
457 | date_buffer, (int)strlen(ptr), datatype);
|
---|
458 | (void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer));
|
---|
459 | }
|
---|
460 | if(WebDID_Debug)
|
---|
461 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
462 | (void)web_write(conn_id,ptr,(int)strlen(ptr));
|
---|
463 | if(WebDID_Debug == 2)
|
---|
464 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | void sendSmiData(int conn_id, char *buffer, int type)
|
---|
469 | {
|
---|
470 | static char date_buffer[128];
|
---|
471 | static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
472 | static char snd_data_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
473 | char *ptr = 0;
|
---|
474 | char node[128], server[256], service[256];
|
---|
475 | int pid, ret, req, browser, force;
|
---|
476 | extern char *update_services(), *update_smi_objects();
|
---|
477 | extern char *update_service_data();
|
---|
478 | extern char *getJSONHeader();
|
---|
479 | extern char *getJSONBuffer(), *getJSONSmiBuffer();
|
---|
480 | char datatype[128];
|
---|
481 | char *conv_buffer;
|
---|
482 |
|
---|
483 | conv_buffer = buffer;
|
---|
484 | strcpy(datatype,"application/json");
|
---|
485 | if(type == 0)
|
---|
486 | {
|
---|
487 | ptr = getJSONHeader(1);
|
---|
488 | }
|
---|
489 | else if(type == 1)
|
---|
490 | {
|
---|
491 | ret = getNodeParameters(conv_buffer, node, &browser);
|
---|
492 | ptr = getJSONSmiBuffer(node, browser);
|
---|
493 | }
|
---|
494 | else if(type == 2)
|
---|
495 | {
|
---|
496 | ret = getServerParameters(conv_buffer, node, server, &pid, &browser);
|
---|
497 | if(!ret)
|
---|
498 | {
|
---|
499 | strcpy(snd_data_buffer,"{}");
|
---|
500 | ptr = snd_data_buffer;
|
---|
501 | }
|
---|
502 | else
|
---|
503 | {
|
---|
504 | ptr = update_smi_objects(node, server, pid, browser);
|
---|
505 | }
|
---|
506 | }
|
---|
507 | else if(type == 3)
|
---|
508 | {
|
---|
509 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
510 | if(!ret)
|
---|
511 | {
|
---|
512 | strcpy(snd_data_buffer,"{}");
|
---|
513 | ptr = snd_data_buffer;
|
---|
514 | }
|
---|
515 | else
|
---|
516 | {
|
---|
517 | update_service_data(service, conn_id, 0, req, browser, force, 0, 0);
|
---|
518 | return;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | else if(type == 4)
|
---|
522 | {
|
---|
523 | ptr = conv_buffer;
|
---|
524 | if(WebDID_Debug)
|
---|
525 | printf("%s\n",ptr);
|
---|
526 | strcpy(datatype,"text/html");
|
---|
527 | }
|
---|
528 | else if(type == 5)
|
---|
529 | {
|
---|
530 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
531 | if(!ret)
|
---|
532 | {
|
---|
533 | strcpy(snd_data_buffer,"{}");
|
---|
534 | ptr = snd_data_buffer;
|
---|
535 | }
|
---|
536 | else
|
---|
537 | {
|
---|
538 | check_browser_changes(service, conn_id, 0, req, browser, force);
|
---|
539 | return;
|
---|
540 | }
|
---|
541 | }
|
---|
542 | else if(type == 6)
|
---|
543 | {
|
---|
544 | ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
|
---|
545 | if(!ret)
|
---|
546 | {
|
---|
547 | strcpy(snd_data_buffer,"{}");
|
---|
548 | ptr = snd_data_buffer;
|
---|
549 | }
|
---|
550 | else
|
---|
551 | {
|
---|
552 | find_services(service, conn_id, browser, force);
|
---|
553 | if(force == -1)
|
---|
554 | strcpy(snd_data_buffer,"");
|
---|
555 | else
|
---|
556 | strcpy(snd_data_buffer,"load");
|
---|
557 | ptr = snd_data_buffer;
|
---|
558 | }
|
---|
559 | }
|
---|
560 | if (IsWebDID)
|
---|
561 | {
|
---|
562 | getTime(date_buffer);
|
---|
563 | (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",
|
---|
564 | date_buffer, (int)strlen(ptr), datatype);
|
---|
565 | (void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer));
|
---|
566 | }
|
---|
567 | if(WebDID_Debug)
|
---|
568 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
569 | (void)web_write(conn_id,ptr,(int)strlen(ptr));
|
---|
570 | if(WebDID_Debug == 2)
|
---|
571 | printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
|
---|
572 | }
|
---|
573 |
|
---|
574 | void sendFile(int conn_id, char *buffer, int size)
|
---|
575 | {
|
---|
576 | int j, file_fd, buflen, len;
|
---|
577 | int i, ret;
|
---|
578 | char * fstr;
|
---|
579 | int flen;
|
---|
580 | static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
|
---|
581 | static char date_buffer[128];
|
---|
582 | char *ptr;
|
---|
583 | int operation = 0;
|
---|
584 |
|
---|
585 |
|
---|
586 | ret = size;
|
---|
587 | if(ret > 0 && ret < BUFSIZE) /* return code is valid chars */
|
---|
588 | buffer[ret]=0; /* terminate the buffer */
|
---|
589 | else buffer[0]=0;
|
---|
590 |
|
---|
591 | if(WebDID_Debug)
|
---|
592 | printf("Got %s\n", buffer);
|
---|
593 | if (IsWebDID)
|
---|
594 | {
|
---|
595 | if (strncmp(buffer, "GET ", 4) && strncmp(buffer, "get ", 4))
|
---|
596 | {
|
---|
597 | log_it(SORRY, "Only simple GET operation supported", buffer, conn_id);
|
---|
598 | return;
|
---|
599 | }
|
---|
600 | }
|
---|
601 | for(i=4;i<BUFSIZE;i++)
|
---|
602 | { /* null terminate after the second space to ignore extra stuff */
|
---|
603 | if(buffer[i] == ' ')
|
---|
604 | { /* string is "GET URL " +lots of other stuff */
|
---|
605 | buffer[i] = 0;
|
---|
606 | break;
|
---|
607 | }
|
---|
608 | }
|
---|
609 |
|
---|
610 | if(WebDID_Debug)
|
---|
611 | printf("Got 1 %s\n", buffer);
|
---|
612 | for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */
|
---|
613 | {
|
---|
614 | if(buffer[j] == '.' && buffer[j+1] == '.')
|
---|
615 | {
|
---|
616 | log_it(SORRY,"Parent directory (..) path names not supported",buffer,conn_id);
|
---|
617 | return;
|
---|
618 | }
|
---|
619 | }
|
---|
620 | if((int)strlen(buffer) == 5)
|
---|
621 | {
|
---|
622 | if( !strncmp(&buffer[0],"GET /",5) || !strncmp(&buffer[0],"get /",5) )
|
---|
623 | /* convert no filename to index file */
|
---|
624 | (void)strcpy(buffer,"GET /index.html");
|
---|
625 | }
|
---|
626 | if((int)strlen(buffer) == 8)
|
---|
627 | {
|
---|
628 | if( !strncmp(&buffer[0],"GET /smi",8) || !strncmp(&buffer[0],"get /smi",8) )
|
---|
629 | /* convert no filename to index file */
|
---|
630 | (void)strcpy(buffer,"GET /smi/index.html");
|
---|
631 | }
|
---|
632 | /* work out the file type and check we support it */
|
---|
633 | buflen=(int)strlen(buffer);
|
---|
634 | fstr = (char *)0;
|
---|
635 | for(i=0;extensions[i].ext != 0;i++)
|
---|
636 | {
|
---|
637 | len = (int)strlen(extensions[i].ext);
|
---|
638 | if( !strncmp(&buffer[buflen-len], extensions[i].ext, (size_t)len))
|
---|
639 | {
|
---|
640 | fstr =extensions[i].filetype;
|
---|
641 | break;
|
---|
642 | }
|
---|
643 | }
|
---|
644 | /*
|
---|
645 | (void)sprintf(snd_buffer,"HTTP/1.1 100 Continue\r\n\r\n");
|
---|
646 | (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
|
---|
647 | printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
648 | */
|
---|
649 | ptr = &buffer[5];
|
---|
650 | if (!IsWebDID)
|
---|
651 | {
|
---|
652 | if (!strncmp(&buffer[0], "GET", 3) || !strncmp(&buffer[0], "get", 3))
|
---|
653 | {
|
---|
654 | operation = 0;
|
---|
655 | }
|
---|
656 | if (!strncmp(&buffer[0], "SUBSCRIBE", 9) || !strncmp(&buffer[0], "subscribe", 9))
|
---|
657 | {
|
---|
658 | operation = 1;
|
---|
659 | ptr = &buffer[11];
|
---|
660 | }
|
---|
661 | if (!strncmp(&buffer[0], "UNSUBSCRIBE", 11) || !strncmp(&buffer[0], "unsubscribe", 11))
|
---|
662 | {
|
---|
663 | operation = -1;
|
---|
664 | ptr = &buffer[13];
|
---|
665 | }
|
---|
666 | }
|
---|
667 | if(fstr == 0)
|
---|
668 | {
|
---|
669 | if(WebDID_Debug)
|
---|
670 | printf("Got %s\n", buffer);
|
---|
671 | if (!strncmp(ptr, "didHeader", 9))
|
---|
672 | {
|
---|
673 | sendData(conn_id, ptr, 0, 0);
|
---|
674 | return;
|
---|
675 | }
|
---|
676 | else if (!strncmp(ptr, "didData", 7))
|
---|
677 | {
|
---|
678 | sendData(conn_id, ptr, 1, 0);
|
---|
679 | return;
|
---|
680 | }
|
---|
681 | else if (!strncmp(ptr, "didServices", 11))
|
---|
682 | {
|
---|
683 | sendData(conn_id, ptr, 2, 0);
|
---|
684 | return;
|
---|
685 | }
|
---|
686 | else if (!strncmp(ptr, "didServiceData", 14))
|
---|
687 | {
|
---|
688 | sendData(conn_id, ptr, 3, 0);
|
---|
689 | return;
|
---|
690 | }
|
---|
691 | else if (!strncmp(ptr, "dimService", 10))
|
---|
692 | {
|
---|
693 | sendData(conn_id, ptr, 10, operation);
|
---|
694 | return;
|
---|
695 | }
|
---|
696 | else if (!strncmp(ptr, "didPoll", 7))
|
---|
697 | {
|
---|
698 | sendData(conn_id, ptr, 5, 0);
|
---|
699 | return;
|
---|
700 | }
|
---|
701 | else if (!strncmp(ptr, "didQuery", 8))
|
---|
702 | {
|
---|
703 | sendData(conn_id, ptr, 6, 0);
|
---|
704 | return;
|
---|
705 | }
|
---|
706 | else if (!strncmp(ptr, "dimBrowser", 10))
|
---|
707 | {
|
---|
708 | sendData(conn_id, ptr, 12, 0);
|
---|
709 | return;
|
---|
710 | }
|
---|
711 | else if (!strncmp(ptr, "smiData", 7))
|
---|
712 | {
|
---|
713 | sendSmiData(conn_id, ptr, 1);
|
---|
714 | return;
|
---|
715 | }
|
---|
716 | else if (!strncmp(ptr, "smiObjects", 10))
|
---|
717 | {
|
---|
718 | sendSmiData(conn_id, ptr, 2);
|
---|
719 | return;
|
---|
720 | }
|
---|
721 | /*
|
---|
722 | if((!strncmp(&buffer[5],"didData",7)) || (!strncmp(&buffer[5],"didServices",11)))
|
---|
723 | {
|
---|
724 | if(ptr = strchr(&buffer[5],'/'))
|
---|
725 | {
|
---|
726 | *ptr = '\0';
|
---|
727 | }
|
---|
728 | buflen=(int)strlen(buffer);
|
---|
729 | for(i=0;extensions[i].ext != 0;i++)
|
---|
730 | {
|
---|
731 | len = (int)strlen(extensions[i].ext);
|
---|
732 | if( !strncmp(&buffer[buflen-len], extensions[i].ext, len))
|
---|
733 | {
|
---|
734 | fstr =extensions[i].filetype;
|
---|
735 | printf("fstr %s", fstr);
|
---|
736 | break;
|
---|
737 | }
|
---|
738 | }
|
---|
739 | if(!strncmp(&buffer[5],"didData",7))
|
---|
740 | {
|
---|
741 | sendData(conn_id, &buffer[5]);
|
---|
742 | }
|
---|
743 | else if(!strncmp(&buffer[5],"didServices",11))
|
---|
744 | {
|
---|
745 | sendData(conn_id, &buffer[5]);
|
---|
746 | }
|
---|
747 | }
|
---|
748 | */
|
---|
749 | else
|
---|
750 | {
|
---|
751 | log_it(SORRY,"file extension type not supported",buffer,conn_id);
|
---|
752 | return;
|
---|
753 | }
|
---|
754 | }
|
---|
755 |
|
---|
756 | if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
|
---|
757 | {
|
---|
758 | log_it(SORRY, "failed to open file",&buffer[5],conn_id);
|
---|
759 | return;
|
---|
760 | }
|
---|
761 |
|
---|
762 | flen = 0;
|
---|
763 | while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 )
|
---|
764 | {
|
---|
765 | flen += ret;
|
---|
766 | }
|
---|
767 | close(file_fd);
|
---|
768 |
|
---|
769 | if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
|
---|
770 | {
|
---|
771 | log_it(SORRY, "failed to open file",&buffer[5],conn_id);
|
---|
772 | return;
|
---|
773 | }
|
---|
774 |
|
---|
775 | getTime(date_buffer);
|
---|
776 | (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",
|
---|
777 | date_buffer, flen, fstr);
|
---|
778 | (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
|
---|
779 | if(WebDID_Debug)
|
---|
780 | printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
|
---|
781 |
|
---|
782 | /* send file in 8KB block - last block may be smaller */
|
---|
783 | while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 ) {
|
---|
784 | (void)web_write(conn_id,snd_buffer,ret);
|
---|
785 | if(WebDID_Debug == 2)
|
---|
786 | printf("SENDING data to conn %d: %d bytes\n",conn_id, ret);
|
---|
787 | }
|
---|
788 | close(file_fd);
|
---|
789 | #ifdef LINUX
|
---|
790 | sleep(1); /* to allow socket to drain */
|
---|
791 | #endif
|
---|
792 | }
|
---|
793 |
|
---|
794 | static void handler( int conn_id, char *packet, int size, int status )
|
---|
795 | {
|
---|
796 | int close_browser();
|
---|
797 |
|
---|
798 | switch(status)
|
---|
799 | {
|
---|
800 | case STA_DISC: /* connection broken */
|
---|
801 | if(WebDID_Debug)
|
---|
802 | {
|
---|
803 | dim_print_date_time();
|
---|
804 | printf(" Disconnect received - conn: %d to %s@%s\n", conn_id,
|
---|
805 | Net_conns[conn_id].task,Net_conns[conn_id].node );
|
---|
806 | }
|
---|
807 | close_browser(conn_id);
|
---|
808 | web_close(conn_id);
|
---|
809 | break;
|
---|
810 | case STA_CONN: /* connection received */
|
---|
811 | if(WebDID_Debug)
|
---|
812 | {
|
---|
813 | dim_print_date_time();
|
---|
814 | printf(" Connection request received - conn: %d\n", conn_id);
|
---|
815 | }
|
---|
816 | break;
|
---|
817 | case STA_DATA: /* normal packet */
|
---|
818 | /*
|
---|
819 | dim_print_date_time();
|
---|
820 | printf(" conn %d packet received:\n", conn_id);
|
---|
821 | printf("packet size = %d\n", size);
|
---|
822 | printf("%s\n",packet);
|
---|
823 | fflush(stdout);
|
---|
824 | */
|
---|
825 | sendFile(conn_id, packet, size);
|
---|
826 | break;
|
---|
827 | default:
|
---|
828 | dim_print_date_time();
|
---|
829 | printf( " - DIM panic: recv_rout(): Bad switch, exiting...\n");
|
---|
830 | abort();
|
---|
831 | }
|
---|
832 | }
|
---|
833 |
|
---|
834 | static void error_handler(int conn_id, int severity, int errcode, char *reason)
|
---|
835 | {
|
---|
836 | if(conn_id){}
|
---|
837 | if(errcode){}
|
---|
838 | dim_print_msg(reason, severity);
|
---|
839 | /*
|
---|
840 | if(severity == 3)
|
---|
841 | {
|
---|
842 | printf("Exiting!\n");
|
---|
843 | exit(2);
|
---|
844 | }
|
---|
845 | */
|
---|
846 | }
|
---|
847 |
|
---|
848 | int main(int argc, char **argv)
|
---|
849 | {
|
---|
850 | int port;
|
---|
851 | int proto;
|
---|
852 | char dns_node[128];
|
---|
853 | int web_get_port();
|
---|
854 | char *ptr;
|
---|
855 | char currwd[256];
|
---|
856 |
|
---|
857 | if(argc){}
|
---|
858 |
|
---|
859 | strcpy(currwd, argv[0]);
|
---|
860 | printf("arg %s\n",currwd);
|
---|
861 | ptr = strrchr(currwd,'/');
|
---|
862 | if(ptr)
|
---|
863 | {
|
---|
864 | *ptr = '\0';
|
---|
865 | }
|
---|
866 | ptr = strrchr(currwd,'\\');
|
---|
867 | if(ptr)
|
---|
868 | {
|
---|
869 | *ptr = '\0';
|
---|
870 | }
|
---|
871 | // chdir(currwd);
|
---|
872 | log_it(LOG,"webDim starting",argv[1],getpid());
|
---|
873 | /* setup the network socket */
|
---|
874 | proto = 1;
|
---|
875 | port = web_get_port();
|
---|
876 | get_node_name(dns_node);
|
---|
877 | did_init(dns_node, DNS_PORT);
|
---|
878 | if(IsWebDID)
|
---|
879 | {
|
---|
880 | if (!web_open_server("DID", handler, &proto, &port, error_handler))
|
---|
881 | return(0);
|
---|
882 | }
|
---|
883 | else
|
---|
884 | {
|
---|
885 | if (!web_open_server("DimClient", handler, &proto, &port, error_handler))
|
---|
886 | return(0);
|
---|
887 | }
|
---|
888 | /*
|
---|
889 | ret = matchString("hello world","*ll*");
|
---|
890 | printf("%s %s %d\n", "hello world","*ll*",ret);
|
---|
891 | ret = matchString("hello world","ll*");
|
---|
892 | printf("%s %s %d\n", "hello world","ll*",ret);
|
---|
893 | */
|
---|
894 | while(1)
|
---|
895 | sleep(10);
|
---|
896 | return(0);
|
---|
897 | }
|
---|