从CGI C模块返回http错误代码 [英] return http error code from CGI C module
问题描述
我有一个用C& C编写的CGI模块。对于某些情况,我想从此模块返回HTTP错误400。问题是 - 我不知道如何从模块返回HTTP错误。
I've a CGI module written in C & for some condition I want to return HTTP error 400 from this module. The problem is - I don't know how to return HTTP error from the module.
在我的模块中看起来像'return(-1)',返回500内部服务器错误。我已经尝试过返回400等但是静脉。我甚至试过printf(状态:400);返回-1之前(如下所示:如何从C ++ CGI程序返回500 HTTP状态)但这不起作用。
Looks like the 'return(-1)' in my module, returns the 500 internal server error. I've tried returning 400 etc. but in vein. I've even tried "printf("Status:400");" before returning -1 (as suggested here: How to return a 500 HTTP status from a C++ CGI program ) but that didn't work.
对此有任何建议将不胜感激。
Any advice on this would be appreciated.
编辑:[已解决]我能够从python模块(稍后由此C CGI模块调用)返回HTTP错误代码。所以没有尝试下面评论中提到的建议。感谢您提供帮助。
[solved] I was able to return HTTP error code from the python module (which is called later by this C CGI module). So didn't get to try the suggestion mentioned in comments below. Thanks for offering help, though.
推荐答案
要将HTTP错误400返回给HTTP客户端,您必须编写HTTP状态行to stdout,像这样:
To return HTTP error 400 to the HTTP client, you have to write the HTTP status line to stdout, like this:
printf("Status: 400 Bad Request\n");
参考: https://tools.ietf.org/html/rfc3875
状态标题字段包含3位整数结果代码
表示脚本尝试处理
请求的成功程度。
The Status header field contains a 3-digit integer result code that indicates the level of success of the script's attempt to handle the request.
Status = "Status:" status-code SP reason-phrase NL
status-code = "200" | "302" | "400" | "501" | extension-code
extension-code = 3digit
reason-phrase = *TEXT
这篇关于从CGI C模块返回http错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!