解决boa网页操作出现502 Bad Gateway The CGI was not CGI/1.1 compliant的一种可能
最近在把一套网页操作的接口从原来Android5.0上移植到Android7.0上。
客户端连接验证的时候主页显示异常
502 Bad Gateway The CGI was not CGI/1.1 compliant
从板子的串口上看到log显示为
[02/Jan/1970:17:13:49 +0000] cgi_header: unable to find LFLF
这部分log为boa里面打印出来的,跟踪源码在cgi_header.c关键函数如下
45 /* TODO: 46 We still need to cycle through the data before the end of the headers, 47 line-by-line, and check for any problems with the CGI 48 outputting overriding http responses, etc... 49 */ 50 51 int process_cgi_header(request * req) 52 { 53 char *buf; 54 char *c; 55 56 if (req->cgi_status != CGI_DONE) 57 req->cgi_status = CGI_BUFFER; 58 59 buf = req->header_line; 60 61 c = strstr(buf, " "); 62 if (c == NULL) { 63 c = strstr(buf, " "); 64 if (c == NULL) { 65 log_error_time(); 66 fputs("cgi_header: unable to find LFLF ", stderr); 67 #ifdef FASCIST_LOGGING 68 log_error_time(); 69 fprintf(stderr, ""%s" ", buf); 70 #endif 71 send_r_bad_gateway(req); 72 return 0; 73 } 74 } 75 if (req->simple) { 76 if (*(c + 1) == ' ') 77 req->header_line = c + 2; 78 else 79 req->header_line = c + 1; 80 return 1; 81 }
该函数
process_cgi_header
是boa父进程fork出子进去去执行对应的cgi程序后,通过管道来获取cgi执行的输出内容进行解析,从而对客户端进行相应。
在src/pipe.c调用如下:
27 /* 28 * Name: read_from_pipe 29 * Description: Reads data from a pipe 30 * 31 * Return values: 32 * -1: request blocked, move to blocked queue 33 * 0: EOF or error, close it down 34 * 1: successful read, recycle in ready queue 35 */ 36 37 int read_from_pipe(request * req) 38 { 39 int bytes_read, bytes_to_read = 40 BUFFER_SIZE - (req->header_end - req->buffer); 41 42 if (bytes_to_read == 0) { /* buffer full */ 43 if (req->cgi_status == CGI_PARSE) { /* got+parsed header */ 44 req->cgi_status = CGI_BUFFER; 45 *req->header_end = '