c语言实现fastcgi

2022-09-14 14:33:02 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

作者 微信公众号:青儿创客基地 B站:主页 https://space.bilibili.com/208826118

参考

fastcgi官网 网关协议CGI、FastCGI、WSGI的区别 uWSGI项目 FastCGI 使用VS2010的nmake命令编译MakeFile流程 visual studio 2017 Community nmake Fastcgi fastcgi c/c API 说明 c 语言写的fastcgi 程序 用C语言开发FastCGI应用程序——fcgi_stdio包API fcgi程序两种编写风格 FastCGI lighttpd开发之介绍和环境搭建 nginx spawn-fcgi demo fcgi库函数 fcgi原理和异步模型 FCGI个人学习记录 HttpFcgi模块

使用

fastcgi官网迁移到了github了,下载FastCGI Developer’s Kit,编译,

代码语言:javascript复制
$ ./autogen.sh
$ ./configure --prefix=/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/install LDFLAGS=-L/lib/w32api
$ make
$ make install
$ ls install
bin  include  lib
$ ls install/bin/
cgi-fcgi.exe  cygfcgi  -0.dll  cygfcgi-0.dll
$ ls install/include/
fastcgi.h  fcgi_config.h  fcgi_stdio.h  fcgiapp.h  fcgimisc.h  fcgio.h  fcgios.h
$ ls install/lib
libfcgi.a  libfcgi.dll.a  libfcgi.la  libfcgi  .a  libfcgi  .dll.a  libfcgi  .la  pkgconfig
$ ls examples/ | grep exe
authorizer.exe
echo.exe
echo-cpp.exe
echo-x.exe
log-dump.exe
size.exe
threaded.exe

配置nginx.conf,

代码语言:javascript复制
location = /cmd { 
   
	fastcgi_pass 127.0.0.1:8088;
	fastcgi_index index.cgi;
	include fastcgi.conf;
}

执行,

代码语言:javascript复制
$ ../spawn-fcgi-1.6.4/src/spawn-fcgi.exe -a 127.0.0.1 -p 8088 -f examples/echo.exe -n
$ spawn-fcgi: child spawned successfully: PID: 282
$ kill 282

浏览器输入http://localhost/cmd

关注一下html输出的这些环境变量,开发一些特殊功能时可能会用到,

代码语言:javascript复制
<title>FastCGI echo</title><h1>FastCGI echo</h1>
Request number 2,  Process ID: 293<p>
No data from standard input.<p>
Request environment:<br>
<pre>
FCGI_ROLE=RESPONDER
SCRIPT_FILENAME=C:dogsoftwarenginx-1.14.2/html/cmd
QUERY_STRING=
REQUEST_METHOD=GET
CONTENT_TYPE=
CONTENT_LENGTH=
SCRIPT_NAME=/cmd
REQUEST_URI=/cmd
DOCUMENT_URI=/cmd
DOCUMENT_ROOT=C:dogsoftwarenginx-1.14.2/html
SERVER_PROTOCOL=HTTP/1.1
REQUEST_SCHEME=http
GATEWAY_INTERFACE=CGI/1.1
SERVER_SOFTWARE=nginx/1.14.2
REMOTE_ADDR=127.0.0.1
REMOTE_PORT=51161
SERVER_ADDR=127.0.0.1
SERVER_PORT=80
SERVER_NAME=localhost
REDIRECT_STATUS=200
HTTP_HOST=localhost
HTTP_CONNECTION=keep-alive
HTTP_CACHE_CONTROL=max-age=0
HTTP_UPGRADE_INSECURE_REQUESTS=1
HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
HTTP_ACCEPT=text/html,application/xhtml xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
HTTP_ACCEPT_ENCODING=gzip, deflate, br
HTTP_ACCEPT_LANGUAGE=zh-CN,zh;q=0.9
</pre><p>
Initial environment:<br>
<pre>
USERDOMAIN=DESKTOP-4KV9JL3
OS=Windows_NT
COMMONPROGRAMFILES=C:Program FilesCommon Files
PROCESSOR_LEVEL=6
PSModulePath=C:Program FilesWindowsPowerShellModules;C:Windowssystem32WindowsPowerShellv1.0Modules
CommonProgramW6432=C:Program FilesCommon Files
CommonProgramFiles(x86)=C:Program Files (x86)Common Files
LANG=zh_CN.UTF-8
TZ=Asia/Shanghai
HOSTNAME=DESKTOP-4KV9JL3
PUBLIC=C:UsersPublic
OLDPWD=/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/bin
USERNAME=qinge
LOGONSERVER=\DESKTOP-4KV9JL3
PROCESSOR_ARCHITECTURE=AMD64
LOCALAPPDATA=C:UsersqingeAppDataLocal
COMPUTERNAME=DESKTOP-4KV9JL3
FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
USER=qinge
!::=::
SYSTEMDRIVE=C:
USERPROFILE=C:Usersqinge
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
SYSTEMROOT=C:Windows
USERDOMAIN_ROAMINGPROFILE=DESKTOP-4KV9JL3
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
NVM_SYMLINK=C:Program Filesnodejs
PWD=/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/bin/bin
HOME=/home/qinge
TMP=/tmp
OneDrive=C:UsersqingeOneDrive
PROCESSOR_REVISION=3d04
FPS_BROWSER_USER_PROFILE_STRING=Default
PROFILEREAD=true
NUMBER_OF_PROCESSORS=4
ProgramW6432=C:Program Files
COMSPEC=C:Windowssystem32cmd.exe
APPDATA=C:UsersqingeAppDataRoaming
SHELL=/bin/bash
TERM=xterm
WINDIR=C:Windows
NVM_HOME=C:UsersqingeAppDataRoamingnvm
ProgramData=C:ProgramData
SHLVL=1
MINTTY_SHORTCUT=/cygdrive/c/Users/Public/Desktop/Cygwin64 Terminal.lnk
PRINTER=OneNote
PROGRAMFILES=C:Program Files
ALLUSERSPROFILE=C:ProgramData
TEMP=/tmp
NO_XILINX_DATA_LICENSE=HIDDEN
DriverData=C:WindowsSystem32DriversDriverData
SESSIONNAME=Console
ProgramFiles(x86)=C:Program Files (x86)
PATH=/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/libfcgi/.libs:/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/libfcgi/.libs:/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/bin/lib:/cygdrive/c/dog/program/cgi/fcgi2-2.4.2/bin/bin:/usr/local/bin:/usr/bin:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Windows/System32/OpenSSH:/cygdrive/c/Program Files/Git/cmd:/cygdrive/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility:/cygdrive/c/Users/qinge/AppData/Roaming/nvm:/cygdrive/c/Program Files/nodejs:/cygdrive/c/Users/qinge/AppData/Local/Microsoft/WindowsApps:/cygdrive/c/Users/qinge/AppData/Roaming/nvm:/cygdrive/c/Program Files/nodejs:/cygdrive/c/Users/qinge/AppData/Local/Programs/Microsoft VS Code/bin:/cygdrive/c/Users/qinge/AppData/Local/Programs/Python/Python37:/cygdrive/c/Users/qinge/AppData/Local/Programs/Python/Python37/Scripts
PS1=[e]0;wa]n[e[32m]u@h [e[33m]w[e[0m]n$ 
HOMEDRIVE=C:
INFOPATH=/usr/local/info:/usr/share/info:/usr/info
HOMEPATH=Usersqinge
ORIGINAL_PATH=/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Windows/System32/OpenSSH:/cygdrive/c/Program Files/Git/cmd:/cygdrive/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility:/cygdrive/c/Users/qinge/AppData/Roaming/nvm:/cygdrive/c/Program Files/nodejs:/cygdrive/c/Users/qinge/AppData/Local/Microsoft/WindowsApps:/cygdrive/c/Users/qinge/AppData/Roaming/nvm:/cygdrive/c/Program Files/nodejs:/cygdrive/c/Users/qinge/AppData/Local/Programs/Microsoft VS Code/bin:/cygdrive/c/Users/qinge/AppData/Local/Programs/Python/Python37:/cygdrive/c/Users/qinge/AppData/Local/Programs/Python/Python37/Scripts
EXECIGNORE=*.dll
_=./cgi-fcgi.exe
BIN_SH=xpg4
DUALCASE=1
</pre><p>

表单

axios发送表单,

代码语言:javascript复制
    onSubmit () { 
   
      console.log('log: '   JSON.stringify(this.form))
      this.$http.get('api/cmd?json='   encodeURIComponent(JSON.stringify(this.form))
      ).then((response) => { 
   
        console.info(decodeURIComponent(response.body))
      }, (response) => { 
   
        console.error(response)
      })
    }

表单在环境变量的QUERY_STRING中,测试,

读写文件

读写文件请按如下方式打开,使用输入输出流不行,待定位。

代码语言:javascript复制
open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR));
open(fileNamePtr, O_WRONLY | O_CREAT, (S_IWGRP | S_IWOTH | S_IWUSR));

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/157506.html原文链接:https://javaforall.cn

0 人点赞