1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/**
* @file minihttpd.c
* @author Mathieu Bunel
* @brief Minimal HTTP 0.9 deamon.
* @version 0.3
* @date 2008
*
* Minimal http server.
* @todo Protect against bad formatted request like GET //bin/sh or GET /../foo.
* @todo Make robust.
* @todo directory handling (if /foo/ is request, send /foo/index.html)
*
* 0.3
* Create a thread on each new connection
*
*
* 0.2
* Able to provide files.
* Answers index.html on 'GET /' request.
* Answers error page if request page is unknown.
*
* 0.1
* Accepts one connection, disconnect after answer.
* GET request without arg, just answer an hello world page.
*
*
*/
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#ifdef WIN32
#include <winsock2.h>
#include <process.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#endif
#ifndef socklen_t
#define socklen_t int
#endif
#ifdef _DEBUG
#define TRACE(a) printf(a)
#define TRACEd(a) printf("%d",a)
#else
#define TRACE(a) /**/
#define TRACEd(a) /**/
#endif
#define PORT 7766
#define DEFAULT_PORT 8080
#define VERSION 0.3
#define SND_BUF_SIZE 128
#define QUERY_BUF_SIZE 1024
#define ERROR_404 "HTTP/0.9 200 OK\r\n\
Content-Type: text/html\r\n\r\n\
<html> \
<head><title>minihttpd</title></head>\
<body>\
minihttpd 0.3<br>\
Error file not found.<br>\
</body>\
</html>\r\n\r\n"
void send_file (FILE * pFile, SOCKET sock)
{
TRACE ("send_file\n");
char buffer[SND_BUF_SIZE];
size_t cSize, cRead, cSend;
if (NULL == pFile)
{
return;
}
/* get file size */
fseek (pFile, 0, SEEK_SET);
fseek (pFile, 0, SEEK_END);
cSize = ftell (pFile);
#ifdef _DEBUG
printf ("size: %uko\n", cSize / 1024);
#endif
/* go back to file start */
fseek (pFile, 0, SEEK_SET);
while (0 != cSize)
{
/* read portion of file */
cRead =
fread (buffer, SND_BUF_SIZE < cSize ? SND_BUF_SIZE : cSize, 1,
pFile);
if (cRead < 1)
{
perror ("fread()");
break;
}
/* send file */
cSend =
send (sock, buffer, SND_BUF_SIZE < cSize ? SND_BUF_SIZE : cSize, 0);
if (cSend < 1)
{
perror ("send()");
break;
}
if (cSize > SND_BUF_SIZE)
{
cSize -= SND_BUF_SIZE;
}
else
{
cSize = 0;
break;
}
}
}
void parse_request (char *pRequest, size_t size, SOCKET sock)
{
TRACE ("parse_request(");
TRACE (pRequest);
TRACE (", ");
TRACEd (size);
TRACE (")\n");
char reqPage[SND_BUF_SIZE] = { 0 };
size_t i = 0;
FILE *pFile;
if ((NULL == pRequest) || (size < 1))
{
return;
}
if (0 == strncmp ("GET /", pRequest, 5))
{
/* Can't use strtok, pRequest might not have '\0' delimiter */
for (i = 5; i < size; ++i)
{
if (' ' == pRequest[i])
{
break;
}
}
}
if (i > 5)
{
strncpy (reqPage, pRequest + 5, i - 5);
}
else
{
strcpy (reqPage, "index.html");
printf ("Default page requested... sending index.html\n");
}
pFile = fopen (reqPage, "rb");
if (NULL != pFile)
{
#ifdef _DEBUG
printf ("Page \"%s\" requested\n", reqPage);
#endif
send_file (pFile, sock);
fclose (pFile);
}
else
{
send (sock, ERROR_404, strlen (ERROR_404), 0);
printf ("Page \"%s\" not found\n", reqPage);
}
}
/** can be a thread or a function */
void *wait_query (void *pcsock)
{
if (pcsock != NULL)
{
SOCKET csock = *(SOCKET *) pcsock;
printf ("csock = %d\n", (int) csock);
/* Waiting query */
{
char buffer[QUERY_BUF_SIZE];
int cReceived = recv (csock, buffer, QUERY_BUF_SIZE - 1, 0);
if (cReceived < 0)
{
perror ("recv()");
}
else
{
buffer[cReceived] = 0;
parse_request (buffer, cReceived, csock);
}
}
/* closing connection to client */
closesocket (csock);
}
return NULL;
}
/**
* init function.
* today, on ly used to init Win32 sockets
*/
void init (void)
{
#ifdef WIN32
/* init socket windows */
WSADATA wsa;
WSAStartup (MAKEWORD (2, 2), &wsa);
#endif
}
/**
* init function.
* today, on ly used to desinit Win32 sockets
*/
void desinit (void)
{
#ifdef WIN32
/* desinit socket windows */
WSACleanup ();
printf ("system desinited\n");
#endif
}
void make_file (char const *name, int lignes)
{
FILE *fp = fopen (name, "w");
if (fp != NULL)
{
int i;
fprintf (fp, "<html>\n");
fprintf (fp, " <body>\n");
fprintf (fp, " </p>\n");
for (i = 0; i < lignes; i++)
{
fprintf (fp,
"[%8d] The quick brown fox jumps over the lazy dog<br />",
i);
}
fprintf (fp, " </p>\n");
/* requete */
fprintf (fp, " <p>\n");
fprintf (fp, " <a href=\"index.html\">index.html</a>\n");
fprintf (fp, " </p>\n");
fprintf (fp, " </body>\n");
fprintf (fp, "</html>\n");
fclose (fp), fp = NULL;
}
}
/**
* program entry point
* usage: minihttpd [tcp_port]
*/
int main (int argc, char *argv[])
{
#if 1
make_file ("index.html", 1000);
#endif
/* init */
init ();
printf ("init [OK]\n");
/* desinit preparation */
atexit (desinit);
/* listening socket creation */
SOCKET sock = socket (AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sock)
{
perror ("socket()");
exit (errno);
}
printf ("socket creation [OK]\n");
SOCKADDR_IN sin /*= { 0 }*/ ;
sin.sin_addr.s_addr = htonl (INADDR_ANY);
sin.sin_family = AF_INET;
if (1 == argc)
{
sin.sin_port = htons (DEFAULT_PORT);
}
else
{
sin.sin_port = htons (strtol (argv[1], NULL, 10));
}
if (SOCKET_ERROR == bind (sock, (SOCKADDR *) & sin, sizeof (sin)))
{
perror ("bind()");
exit (errno);
}
printf ("configuration [OK]\n");
/* Listening */
if (SOCKET_ERROR == listen (sock, 1))
{
perror ("listen()");
exit (errno);
}
while (1)
{
/* Acknowledgment */
SOCKADDR_IN csin;
SOCKET csock;
socklen_t sinsize = sizeof csin;
csock = accept (sock, (SOCKADDR *) & csin, &sinsize);
if (INVALID_SOCKET == csock)
{
perror ("accept()");
exit (errno);
}
#ifdef WAIT_QUERY_IN_A_THREAD
{
printf ("WAIT_QUERY_IN_A_THREAD\n");
pthread_t thread;
pthread_create (&thread, NULL, wait_query, &csock);
}
#else
wait_query (&csock);
#endif
}
/* closing listen socket */
closesocket (sock);
printf ("socket closed\n");
return EXIT_SUCCESS;
} |
Partager