fastcgi c/c++ API 说明
fastcgi c/c++ API 下载地址:https://github.com/FastCGI-Archives
先上example
- #include <iostream>
- #include "fcgio.h"
- using namespace std;
- int main(void) {
- // Backup the stdio streambufs
- streambuf * cin_streambuf = cin.rdbuf();
- streambuf * cout_streambuf = cout.rdbuf();
- streambuf * cerr_streambuf = cerr.rdbuf();
- FCGX_Request request;
- FCGX_Init();
- FCGX_InitRequest(&request, , );
- while (FCGX_Accept_r(&request) == ) {
- fcgi_streambuf cin_fcgi_streambuf(request.in);
- fcgi_streambuf cout_fcgi_streambuf(request.out);
- fcgi_streambuf cerr_fcgi_streambuf(request.err);
- cin.rdbuf(&cin_fcgi_streambuf);
- cout.rdbuf(&cout_fcgi_streambuf);
- cerr.rdbuf(&cerr_fcgi_streambuf);
- cout << "Content-type: text/html\r\n"
- << "\r\n"
- << "<html>\n"
- << " <head>\n"
- << " <title>Hello, World!</title>\n"
- << " </head>\n"
- << " <body>\n"
- << " <h1>Hello, World!</h1>\n"
- << " </body>\n"
- << "</html>\n";
- // Note: the fcgi_streambuf destructor will auto flush
- }
- // restore stdio streambufs
- cin.rdbuf(cin_streambuf);
- cout.rdbuf(cout_streambuf);
- cerr.rdbuf(cerr_streambuf);
- return ;
- }
1.首先 initialize the FCGX library with FCGX_Init()
和initialize FCGX_Request with int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
参数:
request是FCGX_Request指针
sock is a file descriptor returned by FCGX_OpenSocket() or 0 (default)
flags是 当指定sock时设为FCGI_FAIL_ON_INTR,默认0
返回值:成功返回0
2.FCGX_Accept_r阻塞接收循环接收fcgi request,_r版本是FCGX_Accept多线程安全版本
int FCGX_Accept_r(FCGX_Request *request);
参数: request FCGX_Request 指针
返回值:成功返回0
3.FCGX_Request结构体
- typedef struct FCGX_Request {
- int requestId; /* valid if isBeginProcessed */
- int role;
- FCGX_Stream *in;
- FCGX_Stream *out;
- FCGX_Stream *err;
- char **envp;
- /* Don't use anything below here */
- struct Params *paramsPtr;
- int ipcFd; /* < 0 means no connection */
- int isBeginProcessed; /* FCGI_BEGIN_REQUEST seen */
- int keepConnection; /* don't close ipcFd at end of request */
- int appStatus;
- int nWriters; /* number of open writers (0..2) */
- int flags;
- int listen_sock;
- int detached;
- } FCGX_Request;
在FCGX_Accept之前,为FCGX_Request分别给in,out和err分配streambuf
二、Nginx 配置
- events {
- worker_connections ;
- }
- http {
- server {
- listen ;
- server_name localhost;
- location / {
- fastcgi_pass 127.0.0.1:;
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;
- fastcgi_param SERVER_SOFTWARE nginx;
- fastcgi_param QUERY_STRING $query_string;
- fastcgi_param REQUEST_METHOD $request_method;
- fastcgi_param CONTENT_TYPE $content_type;
- fastcgi_param CONTENT_LENGTH $content_length;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param SCRIPT_NAME $fastcgi_script_name;
- fastcgi_param REQUEST_URI $request_uri;
- fastcgi_param DOCUMENT_URI $document_uri;
- fastcgi_param DOCUMENT_ROOT $document_root;
- fastcgi_param SERVER_PROTOCOL $server_protocol;
- fastcgi_param REMOTE_ADDR $remote_addr;
- fastcgi_param REMOTE_PORT $remote_port;
- fastcgi_param SERVER_ADDR $server_addr;
- fastcgi_param SERVER_PORT $server_port;
- fastcgi_param SERVER_NAME $server_name;
- }
- }
- }
Nginx 在80端口监听http服务,把请求发送给 在端口8000监听的fastcgi进程
三、运行代码
- # run nginx using the provided configuration
- sudo nginx -c <path to nginx.conf>
- # compile hello_world
- g++ main_v1.cpp -lfcgi++ -lfcgi -o hello_world
- # spawn the fcgi app on port with no fork
- spawn-fcgi -p -n hello_world
或
spawn-fcgi -a 127.0.0.1 -p 8088 -f hello_world
1.先启动nginx 服务
2.编译fastcgi程序,注意加链接库 fcgi++和fcgi
3.运行spwan-fcgi 指定可执行程序
fastcgi c/c++ API 说明的更多相关文章
- 记录一个nginx的配置
rt #user xiaoju; worker_processes ; #error_log logs/error.log notice; #error_log logs/error.log debu ...
- php 面试题
1.通过哪一个函数,可以把错误转换为异常处理? A:set_error_handlerB:error_reportingC:error2exceptionD:catch 正确答案:A 答案分析:set ...
- 2018年小米高级 PHP 工程师面试题(模拟考试卷)
1.通过哪一个函数,可以把错误转换为异常处理? A:set_error_handler B:error_reporting C:error2exception D:catch 正确答案:A 答案分析: ...
- SSRF_FastCGI
SSRF_FastCGI 目录 SSRF_FastCGI FastCGI协议 SSRF ssrf + fastcgi 参考 FastCGI协议 简介 Fast CGI源自旧版本的CGI 路由/结构图 ...
- 【转】搞清FastCgi与PHP-fpm之间的关系
一.问题:网上有的说,fastcgi是一个协议,php-fpm实现了这个协议: 有的说,php-fpm是fastcgi进程的管理器,用来管理fastcgi进程的: 有的说,php-fpm是php内核的 ...
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...
- CGI,FastCGI,PHP-CGI与PHP-FPM
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- mod_PHP&fastcgi
从宏观上来看,PHP内核的实现与世界上绝大多数的程序一样,接收输入数据, 做相应处理然后输出(返回)结果. 我们编写的代码就是PHP接收的输入数据,PHP内核对我们编写的代码进行解释和运算, 最后返回 ...
- PHP FastCGI RCE Vul
catalog . Introduction . nginx文件类型错误解析漏洞 . 针对直接公网开放的Fast-CGI攻击 . 通过FCGI API动态修改php.ini中的配置实现RCE 1. I ...
随机推荐
- bootstrap-select在angular上的应用
1.bootstrap-select 依赖bootstrap.js ,又依赖jQuery,这些都可以用requirejs来处理. 2.一般bootstrap-select 都放在具体的模块上,而是动态 ...
- Spring 跨域请求
1.Jsp的跨域请求 后台jsp代码: <%@ page language="java" contentType="text/html; charset=UTF-8 ...
- Ajax发送GET、POST请求和响应XML数据案例
1.新建工程 新建一个java web工程,新建一个Servlet文件 AServlet.java,用于返回get和post请求. public class AServlet extends Http ...
- No mapping found for HTTP request with URI异常的原因,<mvc:default-servlet-handler/>的作用
一.最近做的一个项目有很多静态资源文件,按照平时的配置springmvc进行配置发现访问不到静态文件,并且在我配置好controller去访问结果还是404 No mapping found for ...
- MySQL root 密码修改
小伙伴要在以前的服务器上装个代码版本控制的软件,要用到数据库,可是想来找去root密码还是忘了,其他已经安装的服务都是用的专用账户配置文件里要找不到root用户的密码.用以下方法将密码强制修改掉: 1 ...
- 深入理解jQuery插件开发总结(二)
1,开始 可以通过为jQuery.fn增加一个新的函数来编写jQuery插件.属性的名字就是你的插件的名字: jQuery.fn.myPlugin = function(){ //开始写你的代码吧! ...
- 从零开始的全栈工程师——html篇1.7
position定位与表单 一.position 1.Position细说 Position:relative; Left:100px; Top:100px; Position:absolute; L ...
- hadoop 3.0.0 alpha3 安装、配置
1. 官网下载 wget http://mirror.bit.edu.cn/apache/hadoop/common /hadoop-3.0.0-alpha3/hadoop-3.0.0-alpha3 ...
- 01_NIO基本概念
[NIO的几个概念] Buffer(缓冲区) Channel(通道,管道) Selector(选择器,多路复用器) [Buffer] Buffer是一个对象,它包括一些要写入或者要读取的数据.在NIO ...
- Ta们,用云计算改变着更多普通人的生活,所以,我们1218
维族音乐的传承者:为家园建设生态农业:为50万货运司机谋福利:电视游戏行业复兴的倡导者:......还有很多平凡普通的人,不同的主角.不同的情节,用自己的云上轨迹在点滴改变着我们的周遭世界.所以,我们 ...