用c ++编写的cgi可以直接输出到寡妇或控制台,是什么? [英] To what does a cgi writtenn in c++ direct output, a widow or the console?

查看:120
本文介绍了用c ++编写的cgi可以直接输出到寡妇或控制台,是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立自己的第一个网站,并决定编写自己的cgi来处理表单和浏览器响应.我知道在Windows中,编程输出定向到窗口,而在控制台中,编程输出定向到控制台.编写cgi时,直接输出对浏览器意味着什么?

I am in the process of building my first website and have decided to write my own cgi for form handling and browser response. I know that in windows programming output is directed to a window and in console programming output is directed to the console. To what does one direct output meant for the browser when writing a cgi?

推荐答案

只需打印到控制台,并记住设置正确的Content-Type.
Just print to the console, and remember to set the right Content-Type.


为您节省一些以后的调试/麻烦事.

1)在输出数据之前,必须具有正确的http标头,并且它们包含输出中的字符数.因此,在最终将其输出到"stdout"之前,您需要在应用程序内部缓冲"所有输出.

2)C/C ++换行符("\ n")在缓冲区中仅计为1个字符,但在"stdout"上打印时将变为两个字符,因此请单独计算换行数并将其添加到缓冲输出的长度.

下面是我的CGI应用程序的底部.我将输出缓冲到total_printout中,并在total_newlines中计算换行符.输出缓冲等的方式取决于您.

To save you some future debugging / headaches.

1) you have to have proper http headers *before* the data is output and they contain the number of characters in the output. Therefore, you need to "buffer up" all your output inside the application before finally dumping it to ''stdout''

2) C/C++ Newline (''\n'') will only count as 1 character in a buffer but will become two characters when you print it on ''stdout'' so count the number of newlines separately and add that to the length of the buffered output.

Below is the bottom of my CGI app. I buffer the output into total_printout and count newlines in total_newlines. How you do your output buffering and the like is up to you.

printf("Content-type: text/html\n");        // make receiving browser happy
if (total_printout != NULL)
{
    printf("Content-length: %d\n\n", strlen(total_printout)+total_newlines);
    printf("%s", total_printout);           // now send it
    free(total_printout);                   // return accumulated output
}
else
{
    printf("Content-length: 0\n\n");        // no output
}


这篇关于用c ++编写的cgi可以直接输出到寡妇或控制台,是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆