📄 httpserv.c
字号:
/*
* Copyright (c) 2004, Dennis Kuschel.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/**
* @file httpserv.c
* @author Dennis Kuschel
* @brief Small HTTP server that works on the Windows filesystem.
*
* This software is from http://mycpu.mikrocontroller.net.
* Please send questions and bug reports to dennis_k@freenet.de.
*/
#include "../src/sys.h"
#include "../src/httpd.h"
#include "sys/types.h"
#include "sys/stat.h"
#ifndef NO_EFS
#error The define NO_EFS is not set in the compiler call!
#endif
/*---------------------------------------------------------------------------
* FUNCTION PROTOTYPES
*-------------------------------------------------------------------------*/
/* local */
static DWORD WINAPI timer_thread(LPVOID arg);
static DWORD WINAPI http_thread(LPVOID arg);
static void start_thread(LPTHREAD_START_ROUTINE function, void* arg);
static sint_t ask(char *question);
/*---------------------------------------------------------------------------
* GLOBAL VARIABLES
*-------------------------------------------------------------------------*/
u32_t jiffies = 0;
sint_t stopTimerThread_g;
/*---------------------------------------------------------------------------
* IMPLEMENTATION OF FUNCTIONS
*-------------------------------------------------------------------------*/
static sint_t ask(char *question)
{
char inbuf[100];
do
{
printf("%s <Y/N>: ", question);
gets(inbuf);
}
while ((strlen(inbuf) != 1) ||
((tolower(inbuf[0]) != 'y') && (tolower(inbuf[0]) != 'n')));
return (tolower(inbuf[0]) == 'y') ? 1 : 0;
}
/*-------------------------------------------------------------------------*/
static DWORD WINAPI timer_thread(LPVOID arg)
{
(void) arg;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
while (!stopTimerThread_g)
{
sysSleep(10);
jiffies++;
}
return 0;
}
static DWORD WINAPI http_thread(LPVOID arg)
{
httpd_start((char*) arg, 0, 0);
return 0;
}
static void start_thread(LPTHREAD_START_ROUTINE function, void* arg)
{
DWORD threadid;
CreateThread(NULL, 0, function, arg, 0, &threadid);
}
/*-------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
char path[256];
/* get HTTP root path from command line */
if (argc > 1)
{
strcpy(path, argv[1]);
}
else
{
path[0] = 0;
}
printf("\nThis is a small HTTP webserver that works on the local filesystem.\n");
printf("You can specify the servers root path as first parameter to this\n");
printf("program on the command line.\n");
printf("The current HTTP root path is: ");
if (*path == 0) {
printf("(current work directory)\n\n");
} else {
printf("%s\n\n", path);
}
/* initialize the socket layer */
wVersionRequested = MAKEWORD(2,0);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
printf("Sorry, no usable winsock.dll found.\n");
return 1;
}
if ((LOBYTE(wsaData.wVersion) != 2) ||
(HIBYTE(wsaData.wVersion) != 0))
{
printf("Sorry, no usable winsock.dll found.\n");
WSACleanup();
return 1;
}
/* create timer thread */
stopTimerThread_g = 0;
start_thread(timer_thread, NULL);
/* start the HTTP demon */
start_thread(http_thread, path);
sysSleep(1500);
if (!httpd_running())
{
printf("Failed to start HTTPD, quit.\n");
return 1;
}
while (ask("HTTPD is running, stop now?") == 0);
printf("\nShutting down HTTPD...");
if (httpd_stop() == 0) {
printf(" OK\n");
} else {
printf(" FAILED\n");
}
/* stop the timer thread */
stopTimerThread_g = 1;
sysSleep(20);
WSACleanup();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -