fastcgi c/c++ API 下载地址:https://github.com/FastCGI-Archives

先上example

  1. #include <iostream>
  2. #include "fcgio.h"
  3.  
  4. using namespace std;
  5.  
  6. int main(void) {
  7. // Backup the stdio streambufs
  8. streambuf * cin_streambuf = cin.rdbuf();
  9. streambuf * cout_streambuf = cout.rdbuf();
  10. streambuf * cerr_streambuf = cerr.rdbuf();
  11.  
  12. FCGX_Request request;
  13.  
  14. FCGX_Init();
  15. FCGX_InitRequest(&request, , );
  16.  
  17. while (FCGX_Accept_r(&request) == ) {
  18. fcgi_streambuf cin_fcgi_streambuf(request.in);
  19. fcgi_streambuf cout_fcgi_streambuf(request.out);
  20. fcgi_streambuf cerr_fcgi_streambuf(request.err);
  21.  
  22. cin.rdbuf(&cin_fcgi_streambuf);
  23. cout.rdbuf(&cout_fcgi_streambuf);
  24. cerr.rdbuf(&cerr_fcgi_streambuf);
  25.  
  26. cout << "Content-type: text/html\r\n"
  27. << "\r\n"
  28. << "<html>\n"
  29. << " <head>\n"
  30. << " <title>Hello, World!</title>\n"
  31. << " </head>\n"
  32. << " <body>\n"
  33. << " <h1>Hello, World!</h1>\n"
  34. << " </body>\n"
  35. << "</html>\n";
  36.  
  37. // Note: the fcgi_streambuf destructor will auto flush
  38. }
  39.  
  40. // restore stdio streambufs
  41. cin.rdbuf(cin_streambuf);
  42. cout.rdbuf(cout_streambuf);
  43. cerr.rdbuf(cerr_streambuf);
  44.  
  45. return ;
  46. }

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结构体

  1. typedef struct FCGX_Request {
  2. int requestId; /* valid if isBeginProcessed */
  3. int role;
  4. FCGX_Stream *in;
  5. FCGX_Stream *out;
  6. FCGX_Stream *err;
  7. char **envp;
  8.  
  9. /* Don't use anything below here */
  10.  
  11. struct Params *paramsPtr;
  12. int ipcFd; /* < 0 means no connection */
  13. int isBeginProcessed; /* FCGI_BEGIN_REQUEST seen */
  14. int keepConnection; /* don't close ipcFd at end of request */
  15. int appStatus;
  16. int nWriters; /* number of open writers (0..2) */
  17. int flags;
  18. int listen_sock;
  19. int detached;
  20. } FCGX_Request;

在FCGX_Accept之前,为FCGX_Request分别给in,out和err分配streambuf

二、Nginx 配置

  1. events {
  2. worker_connections ;
  3. }
  4.  
  5. http {
  6. server {
  7. listen ;
  8. server_name localhost;
  9.  
  10. location / {
  11. fastcgi_pass 127.0.0.1:;
  12.  
  13. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  14. fastcgi_param SERVER_SOFTWARE nginx;
  15. fastcgi_param QUERY_STRING $query_string;
  16. fastcgi_param REQUEST_METHOD $request_method;
  17. fastcgi_param CONTENT_TYPE $content_type;
  18. fastcgi_param CONTENT_LENGTH $content_length;
  19. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  20. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  21. fastcgi_param REQUEST_URI $request_uri;
  22. fastcgi_param DOCUMENT_URI $document_uri;
  23. fastcgi_param DOCUMENT_ROOT $document_root;
  24. fastcgi_param SERVER_PROTOCOL $server_protocol;
  25. fastcgi_param REMOTE_ADDR $remote_addr;
  26. fastcgi_param REMOTE_PORT $remote_port;
  27. fastcgi_param SERVER_ADDR $server_addr;
  28. fastcgi_param SERVER_PORT $server_port;
  29. fastcgi_param SERVER_NAME $server_name;
  30. }
  31. }
  32. }

Nginx 在80端口监听http服务,把请求发送给 在端口8000监听的fastcgi进程

三、运行代码

  1. # run nginx using the provided configuration
  2. sudo nginx -c <path to nginx.conf>
  3.  
  4. # compile hello_world
  5. g++ main_v1.cpp -lfcgi++ -lfcgi -o hello_world
  6.  
  7. # spawn the fcgi app on port with no fork
  8. 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 说明的更多相关文章

  1. 记录一个nginx的配置

    rt #user xiaoju; worker_processes ; #error_log logs/error.log notice; #error_log logs/error.log debu ...

  2. php 面试题

    1.通过哪一个函数,可以把错误转换为异常处理? A:set_error_handlerB:error_reportingC:error2exceptionD:catch 正确答案:A 答案分析:set ...

  3. 2018年小米高级 PHP 工程师面试题(模拟考试卷)

    1.通过哪一个函数,可以把错误转换为异常处理? A:set_error_handler B:error_reporting C:error2exception D:catch 正确答案:A 答案分析: ...

  4. SSRF_FastCGI

    SSRF_FastCGI 目录 SSRF_FastCGI FastCGI协议 SSRF ssrf + fastcgi 参考 FastCGI协议 简介 Fast CGI源自旧版本的CGI 路由/结构图 ...

  5. 【转】搞清FastCgi与PHP-fpm之间的关系

    一.问题:网上有的说,fastcgi是一个协议,php-fpm实现了这个协议: 有的说,php-fpm是fastcgi进程的管理器,用来管理fastcgi进程的: 有的说,php-fpm是php内核的 ...

  6. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?

    什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...

  7. CGI,FastCGI,PHP-CGI与PHP-FPM

    CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...

  8. mod_PHP&fastcgi

    从宏观上来看,PHP内核的实现与世界上绝大多数的程序一样,接收输入数据, 做相应处理然后输出(返回)结果. 我们编写的代码就是PHP接收的输入数据,PHP内核对我们编写的代码进行解释和运算, 最后返回 ...

  9. PHP FastCGI RCE Vul

    catalog . Introduction . nginx文件类型错误解析漏洞 . 针对直接公网开放的Fast-CGI攻击 . 通过FCGI API动态修改php.ini中的配置实现RCE 1. I ...

随机推荐

  1. bootstrap-select在angular上的应用

    1.bootstrap-select 依赖bootstrap.js ,又依赖jQuery,这些都可以用requirejs来处理. 2.一般bootstrap-select 都放在具体的模块上,而是动态 ...

  2. Spring 跨域请求

    1.Jsp的跨域请求 后台jsp代码: <%@ page language="java" contentType="text/html; charset=UTF-8 ...

  3. Ajax发送GET、POST请求和响应XML数据案例

    1.新建工程 新建一个java web工程,新建一个Servlet文件 AServlet.java,用于返回get和post请求. public class AServlet extends Http ...

  4. No mapping found for HTTP request with URI异常的原因,<mvc:default-servlet-handler/>的作用

    一.最近做的一个项目有很多静态资源文件,按照平时的配置springmvc进行配置发现访问不到静态文件,并且在我配置好controller去访问结果还是404 No mapping found for ...

  5. MySQL root 密码修改

    小伙伴要在以前的服务器上装个代码版本控制的软件,要用到数据库,可是想来找去root密码还是忘了,其他已经安装的服务都是用的专用账户配置文件里要找不到root用户的密码.用以下方法将密码强制修改掉: 1 ...

  6. 深入理解jQuery插件开发总结(二)

    1,开始 可以通过为jQuery.fn增加一个新的函数来编写jQuery插件.属性的名字就是你的插件的名字: jQuery.fn.myPlugin = function(){ //开始写你的代码吧! ...

  7. 从零开始的全栈工程师——html篇1.7

    position定位与表单 一.position 1.Position细说 Position:relative; Left:100px; Top:100px; Position:absolute; L ...

  8. 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 ...

  9. 01_NIO基本概念

    [NIO的几个概念] Buffer(缓冲区) Channel(通道,管道) Selector(选择器,多路复用器) [Buffer] Buffer是一个对象,它包括一些要写入或者要读取的数据.在NIO ...

  10. Ta们,用云计算改变着更多普通人的生活,所以,我们1218

    维族音乐的传承者:为家园建设生态农业:为50万货运司机谋福利:电视游戏行业复兴的倡导者:......还有很多平凡普通的人,不同的主角.不同的情节,用自己的云上轨迹在点滴改变着我们的周遭世界.所以,我们 ...