接上篇,这次做了小小的改动和提升。增加了对POST的支持和对其他方法(GET和POST之外的)选择405回复。另外,增加了对CGI的支持,目前可以使用C语言来写(是不是好蠢的赶脚)。相对于上篇,整体做了小小的优化。这次代码就只贴mod_cgi.bas的部分,其他文件我打包了,感兴趣的同学可以下来看看。
注:由于我不是很了解WebServer,写这个东西也是盲人摸象。像什么状态控制、任务调度、容错之类的基本上能省则省,另外也是因为不会写,哈哈。如果有不足之处,还请不吝赐教,右路西裤(霓虹语:请多多指教)!
'mod_cgi.bas
'code by lichmama from cnblogs.com
'CGI支持状态
Public CGI_ENABLED As Boolean
'CGI程序目录
Public Const CGI_ROOT As String = "c:\cgi-bin\" Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As SECURITY_ATTRIBUTES, _
ByVal nSize As Long) As Long Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" ( _
lpStartupInfo As STARTUPINFO) Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" ( _
ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes As Any, _
lpThreadAttributes As Any, _
ByVal bInheritHandles As Boolean, _
ByVal dwCreationFlags As Long, _
lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
lpOverlapped As Any) As Long Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type Private Type OVERLAPPED
ternal As Long
ternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE =
Private Declare Sub RtlZeroMemory Lib "kernel32" (dest As Any, ByVal _
numBytes As Long) Public Function ShellCGI(ByVal head As Object, rep_state As Long) As String
Dim sa As SECURITY_ATTRIBUTES
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim hrp As Long
Dim hwp As Long
Dim ret As Long
Dim envstr As String 'fill this with CGI standard envrionment strings,
' which delimited by chr(0)
envstr = MakeEnvString(head)
Call RtlZeroMemory(ByVal VarPtr(sa), Len(sa))
Call RtlZeroMemory(ByVal VarPtr(si), Len(si))
Call RtlZeroMemory(ByVal VarPtr(pi), Len(pi)) sa.nLength = Len(sa)
sa.lpSecurityDescriptor = &
sa.bInheritHandle = & 'create pipe
ret = CreatePipe(hrp, hwp, sa, &)
If ret = Then
Debug.Print "[HTTP-VBS]: CGI Exception, pipe failed"
Exit Function
End If si.cb = Len(si)
si.hStdOutput = hwp
si.hStdError = hwp
si.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
si.wShowWindow = SW_HIDE 'create the cgi-process, cgi-path: head("Path_Translated")
ret = CreateProcess(head("Path_Translated"), vbNullString, _
ByVal &, ByVal &, True, &, ByVal envstr, vbNullString, si, pi)
If ret = Then
Debug.Print "[HTTP-VBS]: CGI Exception, create process failed"
Exit Function
End If 'read response from cgi
Dim nobr As Long 'num of bytes read
Dim lpbuff As String
Dim szbuff( * ) As Byte
Dim sum As Long
sum =
Call RtlZeroMemory(ByVal VarPtr(szbuff()), * )
Do
nobr = &
lpbuff = String(, " ")
If ReadFile(hrp, ByVal lpbuff, &, nobr, ByVal &) Then
Call RtlMoveMemory(ByVal VarPtr(szbuff(sum)), ByVal StrPtr(lpbuff), LenB(lpbuff))
sum = sum + LenB(lpbuff)
Else
Exit Do
End If
Call CloseHandle(hwp)
Loop
Call CloseHandle(hrp) rep_state =
ShellCGI = Left(szbuff, sum)
End Function Private Function MakeEnvString(ByVal head As Object) As String
MakeEnvString = "REQUEST_METHOD=" & head("Request")("Method") & Chr() & _
"CONTENT_TYPE=" & head("Content-Type") & Chr() & _
"CONTENT_LENGTH=" & head("Content-Length") & Chr() & _
"QUERY_STRING=" & head("Query_String") & Chr() & _
"SCRIPT_NAME=" & head("Script_Name") & Chr() & _
"PATH_INFO=" & head("Path_Info") & Chr() & _
"PATH_TRANSLATED=" & head("Path_Translated") & Chr() & _
"REMOTE_HOST=" & head("Remote_Host") & Chr() & _
"REMOTE_ADDR=" & head("Remote_Addr") & Chr() & _
"REMOTE_PORT=" & head("Remote_Port") & Chr() & _
"REMOTE_USER=" & head("Remote_User") & Chr() & _
"REMOTE_IDENT=" & head("Remote_Ident") & Chr() & _
"AUTH_TYPE=" & head("Auth_Type") & Chr() & _
"SERVER_NAME=http-vb/0.1" & Chr() & _
"SERVER_PORT=80" & Chr() & _
"SERVER_PROTOCOL=HTTP/1.1" & Chr() & _
"DOCUMENT_ROOT=" & head("Document_Root") & Chr() & _
"SERVER_SOFTWARE=http-vb/0.1 vb/6.0" & Chr() & _
"HTTP_ACCEPT=" & head("Accept") & Chr() & _
"HTTP_USER_AGENT=" & head("User-Agent") & Chr() & _
"HTTP_REFERER=" & head("Referer") & Chr() & _
"HTTP_COOKIE=" & head("Cookie") & Chr() & _
"GATEWAY_INTERFACE=CGI/1.1" & Chr()
End Function
CGI的代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define ENV_MAX_LENGTH 0x7fff char *GetEnv(const char *lpName, char *lpbuff){
memset(lpbuff, , ENV_MAX_LENGTH);
GetEnvironmentVariable(lpName, lpbuff, ENV_MAX_LENGTH);
return lpbuff;
} int main(int argc, char *argv[]){
char lpbuff[ENV_MAX_LENGTH]={};
printf("Content-Type: text/html; charset=utf-8\n");
printf("\n");
printf("<html>\n");
printf("<head>\n");
printf("<meta content='text/html; charset=utf-8' http-equiv='content-type' />\n");
printf("<title>cgi page@lichmama</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<ul style='font-family:courier new'>\n");
printf("<li>REQUEST_METHOD: %s</li>\n", GetEnv("REQUEST_METHOD", lpbuff));
printf("<li>CONTENT_TYPE: %s</li>\n", GetEnv("CONTENT_TYPE", lpbuff));
printf("<li>CONTENT_LENGTH: %s</li>\n", GetEnv("CONTENT_LENGTH", lpbuff));
printf("<li>QUERY_STRING: %s</li>\n", GetEnv("QUERY_STRING", lpbuff));
printf("<li>SCRIPT_NAME: %s</li>\n", GetEnv("SCRIPT_NAME", lpbuff));
printf("<li>PATH_INFO: %s</li>\n", GetEnv("PATH_INFO", lpbuff));
printf("<li>PATH_TRANSLATED: %s</li>\n",GetEnv("PATH_TRANSLATED", lpbuff));
printf("<li>REMOTE_HOST: %s</li>\n", GetEnv("REMOTE_HOST", lpbuff));
printf("<li>REMOTE_ADDR: %s</li>\n", GetEnv("REMOTE_ADDR", lpbuff));
printf("<li>REMOTE_PORT: %s</li>\n", GetEnv("REMOTE_PORT", lpbuff));
printf("<li>REMOTE_USER: %s</li>\n", GetEnv("REMOTE_USER", lpbuff));
printf("<li>REMOTE_IDENT: %s</li>\n", GetEnv("REMOTE_IDENT", lpbuff));
printf("<li>AUTH_TYPE: %s</li>\n", GetEnv("AUTH_TYPE", lpbuff));
printf("<li>GATEWAY_INTERFACE: %s</li>\n", GetEnv("GATEWAY_INTERFACE", lpbuff));
printf("<li>SERVER_NAME: %s</li>\n", GetEnv("SERVER_NAME", lpbuff));
printf("<li>SERVER_PORT: %s</li>\n", GetEnv("SERVER_PORT", lpbuff));
printf("<li>SERVER_PROTOCOL: %s</li>\n",GetEnv("SERVER_PROTOCOL", lpbuff));
printf("<li>DOCUMENT_ROOT: %s</li>\n", GetEnv("DOCUMENT_ROOT", lpbuff));
printf("<li>SERVER_SOFTWARE: %s</li>\n",GetEnv("SERVER_SOFTWARE", lpbuff));
printf("<li>HTTP_ACCEPT: %s</li>\n", GetEnv("HTTP_ACCEPT", lpbuff));
printf("<li>HTTP_USER_AGENT: %s</li>\n",GetEnv("HTTP_USER_AGENT", lpbuff));
printf("<li>HTTP_REFERER: %s</li>\n", GetEnv("HTTP_REFERER", lpbuff));
printf("<li>HTTP_COOKIE: %s</li>\n", GetEnv("HTTP_COOKIE", lpbuff));
printf("</ul>\n");
printf("</body>\n");
printf("</html>\n");
return ;
}
贴张图,看看效果(看到.exe会不会觉得邪恶):
咦,怎么添加附件?先来个百度云盘吧。