Documentation Source Text
Check-in [de32cdc42a]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:de32cdc42a3b650c2934d7de6fbd562d06086fa6
Date: 2012-10-23 13:29:07
User: drh
Comment:Added "charset=utf-8" marks to the content type in althttpd.c.
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to misc/althttpd.c

404 exit(0); 404 exit(0); 405 } 405 } 406 406 407 /* 407 /* 408 ** This is called if we timeout. 408 ** This is called if we timeout. 409 */ 409 */ 410 static void Timeout(int NotUsed){ 410 static void Timeout(int NotUsed){ > 411 (void)NotUsed; 411 if( !debugFlag ){ 412 if( !debugFlag ){ 412 strcpy(zReplyStatus, "999"); 413 strcpy(zReplyStatus, "999"); 413 MakeLogEntry(0); 414 MakeLogEntry(0); 414 exit(0); 415 exit(0); 415 } 416 } 416 } 417 } 417 418 418 /* 419 /* 419 ** Tell the client that there is an error in the script. 420 ** Tell the client that there is an error in the script. 420 */ 421 */ 421 static void CgiScriptWritable(void){ 422 static void CgiScriptWritable(void){ 422 StartResponse("500 CGI Configuration Error"); 423 StartResponse("500 CGI Configuration Error"); 423 nOut += printf( 424 nOut += printf( 424 "Content-type: text/html\r\n" | 425 "Content-type: text/plain\r\n" 425 "\r\n" 426 "\r\n" 426 "<head><title>CGI Configuration Error</title></head>\n" < 427 "<body><h1>CGI Configuration Error</h1>\n" < 428 "The CGI program %s is writable by users other than its owner.\n" | 427 "The CGI program %s is writable by users other than its owner.\n", 429 "</body>\n", zRealScript); | 428 zRealScript); 430 MakeLogEntry(0); 429 MakeLogEntry(0); 431 exit(0); 430 exit(0); 432 } 431 } 433 432 434 /* 433 /* 435 ** Tell the client that the server malfunctioned. 434 ** Tell the client that the server malfunctioned. 436 */ 435 */ 437 static void Malfunction(int linenum, const char *zFormat, ...){ 436 static void Malfunction(int linenum, const char *zFormat, ...){ 438 va_list ap; 437 va_list ap; 439 va_start(ap, zFormat); 438 va_start(ap, zFormat); 440 StartResponse("500 Server Malfunction"); 439 StartResponse("500 Server Malfunction"); 441 nOut += printf( 440 nOut += printf( 442 "Content-type: text/html\r\n" | 441 "Content-type: text/plain\r\n" 443 "\r\n" 442 "\r\n" 444 "<head><title>Server Malfunction</title></head>\n" | 443 "Web server malfunctioned; error number %d\n\n", linenum); 445 "<body><h1>Server Malfunction</h1>\n" < 446 "<p>This web server has malfunctioned.\n\n" < 447 "(Error number: %d)</p>\n", linenum); < 448 if( zFormat ){ 444 if( zFormat ){ 449 nOut += printf("<p>"); < 450 nOut += vprintf(zFormat, ap); 445 nOut += vprintf(zFormat, ap); 451 nOut += printf("</p>\n\n"); < 452 } 446 } 453 nOut += printf("</body>\n"); < 454 MakeLogEntry(0); 447 MakeLogEntry(0); 455 exit(0); 448 exit(0); 456 } 449 } 457 450 458 /* 451 /* 459 ** Do a server redirect to the document specified. The document 452 ** Do a server redirect to the document specified. The document 460 ** name not contain scheme or network location or the query string. 453 ** name not contain scheme or network location or the query string. ................................................................................................................................................................................ 662 { "gl", 2, "video/gl" }, 655 { "gl", 2, "video/gl" }, 663 { "gtar", 4, "application/x-gtar" }, 656 { "gtar", 4, "application/x-gtar" }, 664 { "gz", 2, "application/x-gzip" }, 657 { "gz", 2, "application/x-gzip" }, 665 { "hdf", 3, "application/x-hdf" }, 658 { "hdf", 3, "application/x-hdf" }, 666 { "hh", 2, "text/plain" }, 659 { "hh", 2, "text/plain" }, 667 { "hqx", 3, "application/mac-binhex40" }, 660 { "hqx", 3, "application/mac-binhex40" }, 668 { "h", 1, "text/plain" }, 661 { "h", 1, "text/plain" }, 669 { "htm", 3, "text/html" }, | 662 { "htm", 3, "text/html; charset=utf-8" }, 670 { "html", 4, "text/html" }, | 663 { "html", 4, "text/html; charset=utf-8" }, 671 { "ice", 3, "x-conference/x-cooltalk" }, 664 { "ice", 3, "x-conference/x-cooltalk" }, 672 { "ief", 3, "image/ief" }, 665 { "ief", 3, "image/ief" }, 673 { "iges", 4, "model/iges" }, 666 { "iges", 4, "model/iges" }, 674 { "igs", 3, "model/iges" }, 667 { "igs", 3, "model/iges" }, 675 { "ips", 3, "application/x-ipscript" }, 668 { "ips", 3, "application/x-ipscript" }, 676 { "ipx", 3, "application/x-ipix" }, 669 { "ipx", 3, "application/x-ipix" }, 677 { "jad", 3, "text/vnd.sun.j2me.app-descriptor" }, 670 { "jad", 3, "text/vnd.sun.j2me.app-descriptor" }, ................................................................................................................................................................................ 805 { "xyz", 3, "chemical/x-pdb" }, 798 { "xyz", 3, "chemical/x-pdb" }, 806 { "zip", 3, "application/zip" }, 799 { "zip", 3, "application/zip" }, 807 }; 800 }; 808 801 809 for(i=nName-1; i>0 && zName[i]!='.'; i--){} 802 for(i=nName-1; i>0 && zName[i]!='.'; i--){} 810 z = &zName[i+1]; 803 z = &zName[i+1]; 811 len = nName - i; 804 len = nName - i; 812 if( len<sizeof(zSuffix)-1 ){ | 805 if( len<(int)sizeof(zSuffix)-1 ){ 813 strcpy(zSuffix, z); 806 strcpy(zSuffix, z); 814 for(i=0; zSuffix[i]; i++) zSuffix[i] = tolower(zSuffix[i]); 807 for(i=0; zSuffix[i]; i++) zSuffix[i] = tolower(zSuffix[i]); 815 first = 0; 808 first = 0; 816 last = sizeof(aMime)/sizeof(aMime[0]); 809 last = sizeof(aMime)/sizeof(aMime[0]); 817 while( first<=last ){ 810 while( first<=last ){ 818 int c; 811 int c; 819 i = (first+last)/2; 812 i = (first+last)/2; ................................................................................................................................................................................ 894 nIn += strlen(zLine); 887 nIn += strlen(zLine); 895 zMethod = StrDup(GetFirstElement(zLine,&z)); 888 zMethod = StrDup(GetFirstElement(zLine,&z)); 896 zRealScript = zScript = StrDup(GetFirstElement(z,&z)); 889 zRealScript = zScript = StrDup(GetFirstElement(z,&z)); 897 zProtocol = StrDup(GetFirstElement(z,&z)); 890 zProtocol = StrDup(GetFirstElement(z,&z)); 898 if( zProtocol==0 || strncmp(zProtocol,"HTTP/",5)!=0 || strlen(zProtocol)!=8 ){ 891 if( zProtocol==0 || strncmp(zProtocol,"HTTP/",5)!=0 || strlen(zProtocol)!=8 ){ 899 StartResponse("400 Bad Request"); 892 StartResponse("400 Bad Request"); 900 nOut += printf( 893 nOut += printf( 901 "Content-type: text/html\r\n" | 894 "Content-type: text/plain\r\n" 902 "\r\n" 895 "\r\n" 903 "<title>Unknown Protocol On HTTP Request</title>\n" < 904 "<h1>Unknown Protocol</h1>\n" < 905 "This server does not understand the requested protocol\n" 896 "This server does not understand the requested protocol\n" 906 ); 897 ); 907 MakeLogEntry(0); 898 MakeLogEntry(0); 908 exit(0); 899 exit(0); 909 } 900 } 910 if( zScript[0]==0 ) NotFound(__LINE__); 901 if( zScript[0]==0 ) NotFound(__LINE__); 911 if( forceClose ){ 902 if( forceClose ){ ................................................................................................................................................................................ 917 /* This very simple server only understands the GET, POST 908 /* This very simple server only understands the GET, POST 918 ** and HEAD methods 909 ** and HEAD methods 919 */ 910 */ 920 if( strcmp(zMethod,"GET")!=0 && strcmp(zMethod,"POST")!=0 911 if( strcmp(zMethod,"GET")!=0 && strcmp(zMethod,"POST")!=0 921 && strcmp(zMethod,"HEAD")!=0 ){ 912 && strcmp(zMethod,"HEAD")!=0 ){ 922 StartResponse("501 Not Implemented"); 913 StartResponse("501 Not Implemented"); 923 nOut += printf( 914 nOut += printf( 924 "Content-type: text/html\r\n" | 915 "Content-type: text/plain\r\n" 925 "\r\n" 916 "\r\n" 926 "<head><title>Method not implemented</title></head>\n" < 927 "<body><h1>Method not implemented</h1>\n" < 928 "The %s method is not implemented on this server.\n" | 917 "The %s method is not implemented on this server.\n", 929 "</body>\n", < 930 zMethod); 918 zMethod); 931 MakeLogEntry(0); 919 MakeLogEntry(0); 932 exit(0); 920 exit(0); 933 } 921 } 934 922 935 /* Get all the optional fields that follow the first line. 923 /* Get all the optional fields that follow the first line. 936 */ 924 */ ................................................................................................................................................................................ 1037 FILE *out; 1025 FILE *out; 1038 char *zBuf; 1026 char *zBuf; 1039 int n; 1027 int n; 1040 1028 1041 if( len>MAX_CONTENT_LENGTH ){ 1029 if( len>MAX_CONTENT_LENGTH ){ 1042 StartResponse("500 Request too large"); 1030 StartResponse("500 Request too large"); 1043 nOut += printf( 1031 nOut += printf( 1044 "Content-type: text/html\r\n" | 1032 "Content-type: text/plain\r\n" 1045 "\r\n" 1033 "\r\n" 1046 "Too much POST data\n" 1034 "Too much POST data\n" 1047 "</body>\n" < 1048 ); 1035 ); 1049 MakeLogEntry(0); 1036 MakeLogEntry(0); 1050 exit(0); 1037 exit(0); 1051 } 1038 } 1052 sprintf(zTmpNamBuf, "/tmp/-post-data-XXXXXX"); 1039 sprintf(zTmpNamBuf, "/tmp/-post-data-XXXXXX"); 1053 zTmpNam = zTmpNamBuf; 1040 zTmpNam = zTmpNamBuf; 1054 mkstemp(zTmpNam); 1041 mkstemp(zTmpNam); 1055 out = fopen(zTmpNam,"w"); 1042 out = fopen(zTmpNam,"w"); 1056 if( out==0 ){ 1043 if( out==0 ){ 1057 StartResponse("500 Cannot create /tmp file"); 1044 StartResponse("500 Cannot create /tmp file"); 1058 nOut += printf( 1045 nOut += printf( 1059 "Content-type: text/html\r\n" | 1046 "Content-type: text/plain\r\n" 1060 "\r\n" 1047 "\r\n" 1061 "Could not open \"%s\" for writing\n" | 1048 "Could not open \"%s\" for writing\n", zTmpNam 1062 "</body>\n", zTmpNam < 1063 ); 1049 ); 1064 MakeLogEntry(0); 1050 MakeLogEntry(0); 1065 exit(0); 1051 exit(0); 1066 } 1052 } 1067 zBuf = SafeMalloc( len ); 1053 zBuf = SafeMalloc( len ); 1068 if( useTimeout ) alarm(15 + len/2000); 1054 if( useTimeout ) alarm(15 + len/2000); 1069 n = fread(zBuf,1,len,stdin); 1055 n = fread(zBuf,1,len,stdin); ................................................................................................................................................................................ 1267 char zBuf[1000]; 1253 char zBuf[1000]; 1268 Malfunction(__LINE__, "cannot chdir to [%s] from [%s]", 1254 Malfunction(__LINE__, "cannot chdir to [%s] from [%s]", 1269 zDir, getcwd(zBuf,999)); 1255 zDir, getcwd(zBuf,999)); 1270 } 1256 } 1271 1257 1272 /* Setup the environment appropriately. 1258 /* Setup the environment appropriately. 1273 */ 1259 */ 1274 for(i=0; i<sizeof(cgienv)/sizeof(cgienv[0]); i++){ | 1260 for(i=0; i<(int)(sizeof(cgienv)/sizeof(cgienv[0])); i++){ 1275 if( *cgienv[i].pzEnvValue ){ 1261 if( *cgienv[i].pzEnvValue ){ 1276 SetEnv(cgienv[i].zEnvName,*cgienv[i].pzEnvValue); 1262 SetEnv(cgienv[i].zEnvName,*cgienv[i].pzEnvValue); 1277 } 1263 } 1278 } 1264 } 1279 if( useHttps ){ 1265 if( useHttps ){ 1280 putenv("HTTPS=on"); 1266 putenv("HTTPS=on"); 1281 } 1267 } ................................................................................................................................................................................ 1517 sHints.ai_protocol = 0; 1503 sHints.ai_protocol = 0; 1518 rc = getaddrinfo(localOnly ? "localhost": 0, zPort, &sHints, &pAddrs); 1504 rc = getaddrinfo(localOnly ? "localhost": 0, zPort, &sHints, &pAddrs); 1519 if( rc ){ 1505 if( rc ){ 1520 fprintf(stderr, "could not get addr info: %s", 1506 fprintf(stderr, "could not get addr info: %s", 1521 rc!=EAI_SYSTEM ? gai_strerror(rc) : strerror(errno)); 1507 rc!=EAI_SYSTEM ? gai_strerror(rc) : strerror(errno)); 1522 return 1; 1508 return 1; 1523 } 1509 } 1524 for(n=0, p=pAddrs; n<sizeof(listener)/sizeof(listener[0]) && p!=0; | 1510 for(n=0, p=pAddrs; n<(int)(sizeof(listener)/sizeof(listener[0])) && p!=0; 1525 p=p->ai_next){ 1511 p=p->ai_next){ 1526 listener[n] = socket(p->ai_family, p->ai_socktype, p->ai_protocol); 1512 listener[n] = socket(p->ai_family, p->ai_socktype, p->ai_protocol); 1527 if( listener[n]>=0 ){ 1513 if( listener[n]>=0 ){ 1528 /* if we can't terminate nicely, at least allow the socket to be reused */ 1514 /* if we can't terminate nicely, at least allow the socket to be reused */ 1529 setsockopt(listener[n], SOL_SOCKET, SO_REUSEADDR,&opt, sizeof(opt)); 1515 setsockopt(listener[n], SOL_SOCKET, SO_REUSEADDR,&opt, sizeof(opt)); 1530 1516 1531 #if defined(IPV6_V6ONLY) 1517 #if defined(IPV6_V6ONLY)