-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
1342 lines (1210 loc) · 31.4 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id$
* server.c
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/errno.h>
#define SOCKET int
#define INVALID_SOCKET (-1)
#endif
#include <memory.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "server.h"
#include "output.h"
#include "globals.h"
#include "object.h"
#include "exec.h"
#include "timestamp.h"
#include "load.h"
#include "legal.h"
#include "gui.h"
#define MAXSTR 1024 // maximum string length
static int shutdown_server = 0; /**< flag to stop accepting incoming connections */
SOCKET sockfd = (SOCKET)0; /**< socket on which incomming connections are accepted */
/** Callback function to shut server down
This process halts both the server and the simulator.
@return Nothing
**/
static void shutdown_now(void)
{
output_verbose("server shutdown on exit in progress...");
exec_setexitcode(XC_SVRKLL);
shutdown_server = 1;
if (sockfd!=(SOCKET)0)
#ifdef WIN32
shutdown(sockfd,SD_BOTH);
#else
shutdown(sockfd,SHUT_RDWR);
#endif
sockfd = (SOCKET)0;
gui_wait_status(GUIACT_HALT);
output_verbose("server shutdown on exit done");
}
#ifndef WIN32
/** Retrieve the last error code (Linux only)
@returns errno code
**/
int GetLastError()
{
return errno;
}
#endif
void server_request(int); // Function to handle clients' request(s)
void *http_response(void *ptr);
/** Send the data to the client
@returns the number of bytes sent if successful, -1 if failed (errno is set).
**/
static size_t send_data(SOCKET s, char *buffer, size_t len)
{
#ifdef WIN32
return (size_t)send(s,buffer,(int)len,0);
#else
return (size_t)write(s,buffer,len);
#endif
}
/** Receive data from the client (blocking)
@returns the number of bytes received if successful, -1 if failed (errno is set).
**/
static size_t recv_data(SOCKET s,char *buffer, size_t len)
{
#ifdef WIN32
return (size_t)recv(s,buffer,(int)len,0);
#else
return (size_t)read(s,(void *)buffer,len);
#endif
}
/** Main server wait loop
@returns a pointer to the status flag
**/
static unsigned int n_threads = 0;
static pthread_t thread_id;
static void *server_routine(void *arg)
{
static int status = 0;
static int started = 0;
if (started)
{
output_error("server routine is already running");
return NULL;
}
started = 1;
sockfd = (SOCKET)arg;
// repeat forever..
while (!shutdown_server)
{
struct sockaddr_in cli_addr;
SOCKET newsockfd;
int clilen = sizeof(cli_addr);
/* accept client request and get client address */
newsockfd = accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if ((int)newsockfd<0 && errno!=EINTR)
{
status = GetLastError();
output_error("server accept error on fd=%d: code %d", sockfd, status);
goto Done;
}
else if ((int)newsockfd > 0)
{
#ifdef WIN32
output_verbose("accepting incoming connection from %d.%d.%d.%d on port %d", cli_addr.sin_addr.S_un.S_un_b.s_b1,cli_addr.sin_addr.S_un.S_un_b.s_b2,cli_addr.sin_addr.S_un.S_un_b.s_b3,cli_addr.sin_addr.S_un.S_un_b.s_b4,cli_addr.sin_port);
#else
output_verbose("accepting incoming connection from on port %d",cli_addr.sin_port);
#endif
if ( pthread_create(&thread_id,NULL, http_response,(void*)newsockfd)!=0 )
output_error("unable to start http response thread");
if (global_server_quit_on_close)
shutdown_now();
else
gui_wait_status(0);
}
#ifdef NEVER
{
handleRequest(newsockfd);
#ifdef WIN32
closesocket(newsockfd);
#else
close(newsockfd);
#endif
}
#endif
}
output_verbose("server shutdown");
Done:
started = 0;
return (void*)&status;
}
/** Start accepting incoming connections on the designated server socket
@returns SUCCESS/FAILED status code
**/
static pthread_t thread;
STATUS server_startup(int argc, char *argv[])
{
static int started = 0;
int portNumber = global_server_portnum;
SOCKET sockfd;
struct sockaddr_in serv_addr;
#ifdef WIN32
static WSADATA wsaData;
#endif
if (started)
return SUCCESS;
#ifdef WIN32
output_debug("starting WS2");
if (WSAStartup(MAKEWORD(2,0),&wsaData)!=0)
{
output_error("socket library initialization failed: %s",strerror(GetLastError()));
return FAILED;
}
#endif
/* create a new socket */
if ((sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET)
{
output_error("can't create stream socket: %s",strerror(GetLastError()));
return FAILED;
}
atexit(shutdown_now);
memset(&serv_addr,0,sizeof(serv_addr));
Retry:
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portNumber);
/* bind socket to server address */
if (bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
{
if (portNumber<global_server_portnum+1000)
{
portNumber++;
output_warning("server port not available, trying port %d...", portNumber);
goto Retry;
}
#ifdef WIN32
output_error("can't bind to %d.%d.%d.%d",serv_addr.sin_addr.S_un.S_un_b.s_b1,serv_addr.sin_addr.S_un.S_un_b.s_b2,serv_addr.sin_addr.S_un.S_un_b.s_b3,serv_addr.sin_addr.S_un.S_un_b.s_b4);
#else
output_error("can't bind address: %s",strerror(GetLastError()));
#endif
return FAILED;
}
#ifdef WIN32
output_verbose("bind ok to %d.%d.%d.%d",serv_addr.sin_addr.S_un.S_un_b.s_b1,serv_addr.sin_addr.S_un.S_un_b.s_b2,serv_addr.sin_addr.S_un.S_un_b.s_b3,serv_addr.sin_addr.S_un.S_un_b.s_b4);
#else
output_verbose("bind ok to address");
#endif
/* listen for connection */
listen(sockfd,5);
output_verbose("server listening to port %d", portNumber);
global_server_portnum = portNumber;
/* start the server thread */
if (pthread_create(&thread,NULL,server_routine,(void*)sockfd))
{
output_error("server thread startup failed: %s",strerror(GetLastError()));
return FAILED;
}
started = 1;
return SUCCESS;
}
STATUS server_join(void)
{
void *result;
if (pthread_join(thread,&result)==0)
return (STATUS) result;
else
{
output_error("server thread join failed: %s", strerror(GetLastError()));
return FAILED;
}
}
/********************************************************
HTTPCNX routines
*/
typedef struct s_httpcnx {
char query[1024];
char *buffer;
size_t len;
size_t max;
char *status;
char *type;
SOCKET s;
} HTTPCNX;
/** Create an HTTPCNX connection handle
@returns HTTPCNX connection handle pointer on success, NULL on failure
**/
static HTTPCNX *http_create(SOCKET s)
{
HTTPCNX *http = (HTTPCNX*)malloc(sizeof(HTTPCNX));
memset(http,0,sizeof(HTTPCNX));
http->s = s;
http->max = 4096;
http->buffer = malloc(http->max);
return http;
}
/** Reset an HTTPCNX connection handle
This function clears the contents of HTTPCNX connection block so that it can be reused to handle a new message.
@returns Nothing
**/
static void http_reset(HTTPCNX *http)
{
http->status = NULL;
http->type = NULL;
}
#define HTTP_CONTINUE "100 Continue"
#define HTTP_SWITCHPROTOCOL "101 Switching Protocols"
#define HTTP_OK "200 Ok"
#define HTTP_CREATED "201 Created"
#define HTTP_ACCEPTED "202 Accepted"
#define HTTP_NONAUTHORITATIVEINFORMATION "203 Non-Authoritative Information"
#define HTTP_NOCONTENT "204 No Content"
#define HTTP_RESETCONTENT "205 Reset Content"
#define HTTP_PARTIALCONTENT "206 Partial Content"
#define HTTP_MULTIPLECHOICES "300 Multiple Choices"
#define HTTP_MOVEDPERMANENTLY "301 Moved Permanently"
#define HTTP_FOUND "302 Found"
#define HTTP_SEEOTHER "303 See Other"
#define HTTP_NOTMODIFIED "304 Not Modified"
#define HTTP_USEPROXY "305 Use Proxy"
#define HTTP_TEMPORARYREDIRECT "307 Temporary Redirect"
#define HTTP_BADREQUEST "400 Bad Request"
#define HTTP_UNAUTHORIZED "401 Unauthorized"
#define HTTP_PAYMENTREQUIRED "402 Payment Required"
#define HTTP_FORBIDDEN "403 Forbidden"
#define HTTP_NOTFOUND "404 Not Found"
#define HTTP_METHODNOTALLOWED "405 Method Not Allowed"
#define HTTP_NOTACCEPTABLE "406 Not Acceptable"
#define HTTP_PROXYAUTHENTICATIONREQUIRED "407 Proxy Authentication Required"
#define HTTP_REQUESTTIMEOUT "408 Request Time-out"
#define HTTP_CONFLICT "409 Conflict"
#define HTTP_GONE "410 Gone"
#define HTTP_LENGTHREQUIRED "411 Length Required"
#define HTTP_PRECONDITIONFAILED "412 Precondition Failed"
#define HTTP_REQUESTENTITYTOOLARGE "413 Request Entity Too Large"
#define HTTP_REQUESTURITOOLARGE "414 Request-URI Too Large"
#define HTTP_UNSUPPORTEDMEDIATYPE "415 Unsupported Media Type"
#define HTTP_REQUESTEDRANGENOTSATISFIABLE "416 Requested range not satisfiable"
#define HTTP_EXPECTATIONFAILED "417 Expectation Failed"
#define HTTP_INTERNALSERVERERROR "500 Internal Server Error"
#define HTTP_NOTIMPLEMENTED "501 Not Implemented"
#define HTTP_BADGATEWAY "502 Bad Gateway"
#define HTTP_SERVICEUNAVAILABLE "503 Service Unavailable"
#define HTTP_GATEWAYTIMEOUT "504 Gateway Time-out"
#define HTTP_VERSIONNOTSUPPORTED "505 HTTPCNX Version not supported"
/** Set the HTTPCNX response status code **/
static void http_status(HTTPCNX *http, char *status)
{
http->status = status;
}
/** Set the HTTPCNX response message type **/
static void http_type(HTTPCNX *http, char *type)
{
http->type = type;
}
/** Send the HTTPCNX response **/
static void http_send(HTTPCNX *http)
{
char header[4096];
int len=0;
len += sprintf(header+len, "HTTP/1.1 %s", http->status?http->status:HTTP_INTERNALSERVERERROR);
output_verbose("%s (len=%d, mime=%s)",header,http->len,http->type?http->type:"none");
len += sprintf(header+len, "\nContent-Length: %d\n", http->len);
if (http->type && http->type[0]!='\0')
len += sprintf(header+len, "Content-Type: %s\n", http->type);
len += sprintf(header+len, "Cache-Control: no-cache\n");
len += sprintf(header+len, "Cache-Control: no-store\n");
len += sprintf(header+len, "Expires: -1\n");
len += sprintf(header+len,"\n");
send_data(http->s,header,len);
if (http->len>0)
send_data(http->s,http->buffer,http->len);
http->len = 0;
}
/** Write the contents of the HTTPCNX message buffer **/
static void http_write(HTTPCNX *http, char *data, size_t len)
{
if (http->len+len>=http->max)
{
/* extend buffer */
void *old = http->buffer;
if (http->len+len < http->max*2)
{
http->max *= 2;
}
else
{
http->max = http->len+len+1;
}
http->buffer = malloc(http->max);
memcpy(http->buffer,old,http->len);
free(old);
old = NULL;
}
memcpy(http->buffer+http->len,data,len);
http->len += len;
}
/** Close the HTTPCNX connection after sending content **/
static void http_close(HTTPCNX *http)
{
if (http->len>0)
http_send(http);
#ifdef WIN32
closesocket(http->s);
#else
close(http->s);
#endif
}
/** Set the response MIME type **/
static void http_mime(HTTPCNX *http, char *path)
{
size_t len = strlen(path);
static struct s_map {
char *ext;
char *mime;
} map[] = {
{".png","image/png"},
{".js","text/javascript"},
};
int n;
for ( n=0 ; n<sizeof(map)/sizeof(map[0]) ; n++ )
{
if (strcmp(path+len-strlen(map[n].ext),map[n].ext)==0)
{
http->type = map[n].mime;
return;
}
}
output_warning("mime type for '%s' is not known", path);
http->type = NULL;
return;
}
/** Format HTTPCNX message content **/
static int http_format(HTTPCNX *http, char *format, ...)
{
int len;
char data[65536];
va_list ptr;
va_start(ptr,format);
len = vsprintf(data,format,ptr);
va_end(ptr);
http_write(http,data,len);
return len;
}
/** Upquote message buffer content **/
char *http_unquote(char *buffer)
{
char *eob = buffer+strlen(buffer)-1;
if (buffer[0]=='"') buffer++;
if (*eob=='"') *eob='\0';
return buffer;
}
/** Get the value of a hex character
@returns the value corresponding to the hex code
**/
static char hex(char c)
{
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return c-'0';
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return c-'A'+10;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return c-'a'+10;
default:
return 0;
}
}
/** Decode the contents of a buffer (in place) **/
void http_decode(char *buffer)
{
char result[1024], *in, *out = result;
for ( in=buffer ; *in!='\0' ; in++ )
{
if (*in=='%')
{
char hi = *++in;
char lo = *++in;
*out++ = hex(hi)*16 + hex(lo);
}
else
*out++ = *in;
}
*out='\0';
strcpy(buffer,result);
}
/** Process an incoming XML data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_xml_request(HTTPCNX *http,char *uri)
{
char arg1[1024]="", arg2[1024]="";
int nargs = sscanf(uri,"%1023[^/=\r\n]/%1023[^\r\n=]",arg1,arg2);
char *value = strchr(uri,'=');
char buffer[1024]="";
OBJECT *obj=NULL;
char *id;
/* value */
if (value) *value++;
/* decode %.. */
http_decode(arg1);
http_decode(arg2);
if (value) http_decode(value);
/* process request */
switch (nargs) {
/* get global variable */
case 1:
/* find the variable */
if (global_getvar(arg1,buffer,sizeof(buffer))==NULL)
{
output_error("global variable '%s' not found", arg1);
return 0;
}
/* assignment, if any */
if (value) global_setvar(arg1,value);
/* post the response */
http_format(http,"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
http_format(http,"<globalvar>\n\t<name>%s</name>\n\t<value>%s</value>\n</globalvar>\n",
arg1, http_unquote(buffer));
http_type(http,"text/xml");
return 1;
/* get object property */
case 2:
/* find the object */
id = strchr(arg1,':');
if ( id==NULL )
obj = object_find_name(arg1);
else
obj = object_find_by_id(atoi(id+1));
if ( obj==NULL )
{
output_error("object '%s' not found", arg1);
return 0;
}
/* post the current value */
if ( !object_get_value_by_name(obj,arg2,buffer,sizeof(buffer)) )
{
output_error("object '%s' property '%s' not found", arg1, arg2);
return 0;
}
/* assignment, if any */
if ( value && !object_set_value_by_name(obj,arg2,value) )
{
output_error("cannot set object '%s' property '%s' to '%s'", arg1, arg2, value);
return 0;
}
/* post the response */
http_format(http,"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<property>\n");
http_format(http,"\t<object>%s</object>\n", arg1);
http_format(http,"\t<name>%s</name>\n", arg2);
http_format(http,"\t<value>%s</value>\n", http_unquote(buffer));
/* TODO add property type info */
http_format(http,"</property>\n");
http_type(http,"text/xml");
return 1;
default:
return 0;
}
return 0;
}
/** Process an incoming GUI request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_gui_request(HTTPCNX *http,char *uri)
{
gui_set_html_stream((void*)http,http_format);
if (gui_html_output_page(uri)>=0)
{
http_type(http,"text/html");
return 1;
}
else
return 0;
}
#ifndef WIN32
/** Obtain the size of a file (non-Windows only)
@returns the number of bytes in the file or -1 on failure
**/
int filelength(int fd)
{
struct stat fs;
if (fstat(fd,&fs))
return -1;
else
return fs.st_size;
}
#endif
/** Copy the content of a file to the client
@returns the number of bytes sent
**/
int http_copy(HTTPCNX *http, char *context, char *source)
{
char *buffer;
size_t len;
FILE *fp = fopen(source,"rb");
if (fp==NULL)
{
output_error("unable to find %s output '%s': %s", context, source, strerror(errno));
return 0;
}
len = filelength(fileno(fp));
if (len<0)
{
output_error("%s output '%s' not accessible", context, source);
fclose(fp);
return 0;
}
if (len==0)
{
output_warning("%s output '%s' is empty", context, source);
fclose(fp);
return 1;
}
buffer = (char*)malloc(len);
if (buffer==NULL)
{
output_error("%s output buffer for '%s' not available", context, source);
fclose(fp);
return 0;
}
if (fread(buffer,1,len,fp)<=0)
{
output_error("%s output '%s' read failed", context, source);
free(buffer);
fclose(fp);
return 0;
}
http_write(http,buffer,len);
http_mime(http,source);
free(buffer);
fclose(fp);
return 1;
}
/** Process an incoming output data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_output_request(HTTPCNX *http,char *uri)
{
char fullpath[1024];
strcpy(fullpath,global_workdir);
if (*(fullpath+strlen(fullpath)-1)!='/' || *(fullpath+strlen(fullpath)-1)!='\\' )
strcat(fullpath,"/");
strcat(fullpath,uri);
return http_copy(http,"file",fullpath);
}
/** Process an incoming Java request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_run_java(HTTPCNX *http,char *uri)
{
char script[1024];
char command[1024];
char output[1024];
char *mime = strchr(uri,'?');
char *ext = mime?strchr(mime,'/'):NULL;
char *jar = strrchr(uri,'.');
int rc = 0;
/* find mime and extension */
if (mime==NULL)
{
output_error("Java runtime request does not include mime type");
return 0;
}
else
*mime++ = '\0'; /* mime type actually start at next character */
if (ext) ext++;
/* if not a plot request */
if (jar==NULL || strcmp(jar,".jar")!=0)
{
output_error("Java runtime request does not specify is a Java runtime filename with extension .jar");
return 0;
}
/* setup gnuplot command */
sprintf(script,"%s",uri);
sprintf(command,"java -jar %s",script);
/* temporary cut off of plt extension to build output file */
*jar = '\0'; sprintf(output,"%s.%s",uri,ext); *jar='.';
/* run gnuplot */
output_verbose("%s", command);
if ((rc=system(command))!=0)
{
switch (rc)
{
case -1: /* an error occurred */
output_error("unable to run java runtime on '%s': %s", uri, strerror(errno));
break;
default:
output_error("Java runtime return error code %d on '%s'", rc, uri);
break;
}
return 0;
}
/* copy output to http */
return http_copy(http,"Java",output);
}
/** Process an incoming Perl data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_run_perl(HTTPCNX *http,char *uri)
{
char script[1024];
char command[1024];
char output[1024];
char *mime = strchr(uri,'?');
char *ext = mime?strchr(mime,'/'):NULL;
char *pl = strrchr(uri,'.');
int rc = 0;
/* find mime and extension */
if (mime==NULL)
{
output_error("Perl request does not include mime type");
return 0;
}
else
*mime++ = '\0'; /* mime type actually start at next character */
if (ext) ext++;
/* if not a plot request */
if (pl==NULL || strcmp(pl,".pl")!=0)
{
output_error("Perl request does not specify a Perl script filename with extension .pl");
return 0;
}
/* setup gnuplot command */
sprintf(script,"%s",uri);
sprintf(command,"perl %s",script);
/* temporary cut off of plt extension to build output file */
*pl = '\0'; sprintf(output,"%s.%s",uri,ext); *pl='.';
/* run gnuplot */
output_verbose("%s", command);
if ((rc=system(command))!=0)
{
switch (rc)
{
case -1: /* an error occurred */
output_error("unable to run Perl on '%s': %s", uri, strerror(errno));
break;
default:
output_error("Perl return error code %d on '%s'", rc, uri);
break;
}
return 0;
}
/* copy output to http */
return http_copy(http,"Perl",output);
}
/** Process an incoming Python data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_run_python(HTTPCNX *http,char *uri)
{
char script[1024];
char command[1024];
char output[1024];
char *mime = strchr(uri,'?');
char *ext = mime?strchr(mime,'/'):NULL;
char *py = strrchr(uri,'.');
int rc = 0;
/* find mime and extension */
if (mime==NULL)
{
output_error("Python request does not include mime type");
return 0;
}
else
*mime++ = '\0'; /* mime type actually start at next character */
if (ext) ext++;
/* if not a plot request */
if (py==NULL || strcmp(py,".py")!=0)
{
output_error("Python request does not specify a Python script filename with extension .py");
return 0;
}
/* setup gnuplot command */
sprintf(script,"%s",uri);
sprintf(command,"python %s",script);
/* temporary cut off of plt extension to build output file */
*py = '\0'; sprintf(output,"%s.%s",uri,ext); *py='.';
/* run gnuplot */
output_verbose("%s", command);
if ((rc=system(command))!=0)
{
switch (rc)
{
case -1: /* an error occurred */
output_error("unable to run Python on '%s': %s", uri, strerror(errno));
break;
default:
output_error("Python return error code %d on '%s'", rc, uri);
break;
}
return 0;
}
/* copy output to http */
return http_copy(http,"Python",output);
}
/** Process an incoming R data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_run_r(HTTPCNX *http,char *uri)
{
char script[1024];
char command[1024];
char output[1024];
char *mime = strchr(uri,'?');
char *ext = mime?strchr(mime,'/'):NULL;
char *r = strrchr(uri,'.');
int rc = 0;
/* find mime and extension */
if (mime==NULL)
{
output_error("R request does not include mime type");
return 0;
}
else
*mime++ = '\0'; /* mime type actually start at next character */
if (ext) ext++;
/* if not a plot request */
if (r==NULL || strcmp(r,".r")!=0)
{
output_error("R request does not specify an R script filename with extension .r");
return 0;
}
/* setup gnuplot command */
sprintf(script,"%s",uri);
#ifdef WIN32
sprintf(command,"r CMD BATCH %s",script);
#else
sprintf(command,"R --vanilla CMD BATCH %s",script);
#endif
/* temporary cut off of plt extension to build output file */
*r = '\0'; sprintf(output,"%s.%s",uri,ext); *r='.';
/* run gnuplot */
output_verbose("%s", command);
if ((rc=system(command))!=0)
{
switch (rc)
{
case -1: /* an error occurred */
output_error("unable to run R on '%s': %s", uri, strerror(errno));
break;
default:
output_error("R return error code %d on '%s'", rc, uri);
break;
}
return 0;
}
/* copy output to http */
return http_copy(http,"R",output);
}
/** Process an incoming Scilab data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_run_scilab(HTTPCNX *http,char *uri)
{
char script[1024];
char command[1024];
char output[1024];
char *mime = strchr(uri,'?');
char *ext = mime?strchr(mime,'/'):NULL;
char *sce = strrchr(uri,'.');
int rc = 0;
/* find mime and extension */
if (mime==NULL)
{
output_error("Scilab request does not include mime type");
return 0;
}
else
*mime++ = '\0'; /* mime type actually start at next character */
if (ext) ext++;
/* if not a plot request */
if (sce==NULL || strcmp(sce,".sce")!=0)
{
output_error("Scilab request does not specify a Scilab script filename with extension .sce");
return 0;
}
/* setup gnuplot command */
sprintf(script,"%s",uri);
sprintf(command,"scilab %s",script);
/* temporary cut off of plt extension to build output file */
*sce = '\0'; sprintf(output,"%s.%s",uri,ext); *sce='.';
/* run gnuplot */
output_verbose("%s", command);
if ((rc=system(command))!=0)
{
switch (rc)
{
case -1: /* an error occurred */
output_error("unable to run Scilab on '%s': %s", uri, strerror(errno));
break;
default:
output_error("Scilab return error code %d on '%s'", rc, uri);
break;
}
return 0;
}
/* copy output to http */
return http_copy(http,"Scilab",output);
}
/** Process an incoming Octave data request
@returns non-zero on success, 0 on failure (errno set)
**/
int http_run_octave(HTTPCNX *http,char *uri)
{
char script[1024];
char command[1024];
char output[1024];
char *mime = strchr(uri,'?');
char *ext = mime?strchr(mime,'/'):NULL;
char *m = strrchr(uri,'.');
int rc = 0;
/* find mime and extension */
if (mime==NULL)
{
output_error("Octave request does not include mime type");
return 0;
}
else
*mime++ = '\0'; /* mime type actually start at next character */
if (ext) ext++;
/* if not a plot request */