Nut/OS  4.10.3
API Reference
httpd_simple.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009-2011 by egnite GmbH
00003  * Copyright (C) 2001-2004 by egnite Software GmbH
00004  *
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  */
00035 
00047 #include <dev/board.h>
00048 
00049 #include <dev/urom.h>
00050 
00051 #include <sys/version.h>
00052 #include <sys/timer.h>
00053 #include <sys/confnet.h>
00054 #include <sys/socket.h>
00055 
00056 #include <arpa/inet.h>
00057 
00058 #include <pro/dhcp.h>
00059 #include <pro/httpd.h>
00060 
00061 #include <io.h>
00062 #include <errno.h>
00063 
00069 int main(void)
00070 {
00071     uint32_t baud = 115200;
00072     TCPSOCKET *sock;
00073     FILE *stream;
00074 
00075     /*
00076      * Initialize the console.
00077      */
00078     NutRegisterDevice(&DEV_CONSOLE, 0, 0);
00079     freopen(DEV_CONSOLE_NAME, "w", stdout);
00080     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00081 
00082     printf("\n\nSimple HTTP Daemon running on Nut/OS %s\n",
00083            NutVersionString());
00084 
00085     /*
00086      * Initialize the network interface.
00087      */
00088     printf("Configure %s...", DEV_ETHER_NAME);
00089     if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
00090         puts("failed. Cannot register Ethernet device.");
00091         for (;;);
00092     }
00093     if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 60000)) {
00094         puts("failed. Cannot configure network.\nUse editconf.");
00095         for (;;);
00096     }
00097     printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr));
00098 
00099     /*
00100      * Initialize the file system.
00101      */
00102     printf("Register UROM file system...");
00103     if (NutRegisterDevice(&devUrom, 0, 0)) {
00104         puts("failed.");
00105         for (;;);
00106     }
00107     puts("OK.");
00108 
00109     /*
00110      * Now loop endless for connections.
00111      */
00112     for (;;) {
00113         /* Create a socket. */
00114         sock = NutTcpCreateSocket();
00115         if (sock == NULL) {
00116             printf("Error %d creating socket.\n", errno);
00117             NutSleep(1000);
00118             continue;
00119         }
00120 
00121         /* Listen on port 80. NutTcpAccept() will block
00122            until we get a connection from a client. */
00123         printf("Listening...");
00124         NutTcpAccept(sock, 80);
00125         printf("Connected...");
00126 
00127         /* Associate a binary stdio stream with the socket. */
00128         stream = _fdopen((int) ((uintptr_t) sock), "r+b");
00129         if (stream == NULL) {
00130             printf("Error %d creating stream.\n", errno);
00131         } else {
00132             /* This API call saves us a lot of work. It will parse
00133                the client's HTTP request, send the requested file. */
00134             NutHttpProcessRequest(stream);
00135 
00136             /* Destroy the associated stream. */
00137             fclose(stream);
00138         }
00139 
00140         /* Close the socket. */
00141         NutTcpCloseSocket(sock);
00142         puts("Disconnected");
00143     }
00144     return 0;
00145 }