Skip to content

Instantly share code, notes, and snippets.

@yne yne/win_cgi.c
Created Dec 18, 2015

Embed
What would you like to do?
Barebone CGI server for Windows
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
char page[]="HTTP/1.0 200 OK\n\n"
"<!doctype html>"
"<html>"
"<head>"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost/yotsublue.9.css\"></link>"
"<style>/*body{background:#444;color:#FFF;}*/</style>"
"<title>welcome</title>"
"<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\"></script>"
"</head>"
"<body >"//onBeforeUnload=\"document.location.href='/cmd/close';\"
"<script src=\"http://localhost/test.js\"></script>"
"</body></html>";
int alive=1;
void process(int s){
char buf[4096],*meth,*path,*prot,*post=NULL;
int plen=0,len=recv(s,buf,sizeof(buf),0);buf[len]=0;
if(!(meth=strtok(buf," ")))return;
if(!(path=strtok(NULL," ")))return;
if(!(prot=strtok(NULL,"\n")))return;
while(1){
char*line=strtok(NULL, "\n");
if(!line)return;
if(!memcmp(line,"X-Requested-With:",17)){//: XMLHttpRequest
meth="AJAX";
}
if(!strchr(line,':')){//last header line
if(strlen(line)!=1)return;//bad header
plen=len-((line+2)-buf);
if(plen)post=line+2;
// printf("\n=====% 4i=====\n%s\n==============\n",plen,line+2);
break;
}
// printf("%s\n",line);
}
if(!strcmp(meth,"POST")){
printf("posting (%i)%s to %s",plen,post,path);
len=send(s,page,strlen(page),0);
}else if(!strcmp(meth,"GET")){
printf("get page : %s",path);
if(!strcmp(path,"/killitwithfire"))return alive=0;
len=send(s,page,strlen(page),0);
}else if(!strcmp(meth,"AJAX")){
printf("ajax page : %s",path);
//replace("."," ");
int fd=open("tmp",O_RDONLY,0777);
if(fd<0)
return send(s,"HTTP/1.0 404 file err\r\n\r\nnot found",25,0);
send(s,"HTTP/1.0 200 OK\r\n\r\n",19,0);
char tmp[1024];
// memset(tmp,0,1024);
while((len=read(fd,tmp,1024))>0){
// printf(" %i %s",len,tmp);
send(s,tmp,len,0);
}
close(fd);
}else{
printf("unknow method",path);
}
// len=send(s,"lolk�",5,0);
// char c[256];
// read(s,c,256);
// write(s,"HTTP/1.0 200 OK\n\nyo",19);
}
DWORD WINAPI MyThreadFunction( LPVOID lpParam ){
ShellExecuteA(0, "open", "http://localhost:8080",NULL,NULL,SW_SHOWDEFAULT);
}
int /*__cdecl*/ main(int arc,char** argv) {
CreateThread(NULL,0,MyThreadFunction,0,0,NULL);
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData))return printf("WSAStartup failed\n");
int in = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin={AF_INET,htons(8080),INADDR_ANY};
bind(in, (struct sockaddr *) &sin, sizeof(sin));
listen(in,5);
// MessageBox(0,"goto:\nhttp://localhost:8080","info",MB_ICONINFORMATION|MB_OK);
//ShowWindow((struct HWND__ *)GetConsoleWindow(), SW_HIDE );
while (alive){
int out;
printf("\n>");
if((out=accept(in, NULL, NULL)) < 0)break;
process(out);
closesocket(out);
}
closesocket(in);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.