Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

httpd/nutmain.c

Simple HTTP daemon.


#include <dev/nicrtl.h>
#include <dev/uartavr.h>

#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/print.h>

#include <netinet/sostream.h>
#include <arpa/inet.h>

#include <pro/httpd.h>

static NUTDEVICE *uart;

/*
 * CGI Sample. See httpd.h for REQUEST structure.
 */
static int ShowQuery(NUTDEVICE *sostream, REQUEST *req)
{
    NutHttpSendHeaderTop(sostream, req, 200, "Ok");
    NutHttpSendHeaderBot(sostream, "text/html", -1);

    NutPrintString_P(sostream, PSTR("<HTML>"
                                 "<HEAD>"
                                 "<TITLE>Show Query</TITLE>"
                                 "</HEAD>"
                                 "<BODY>"));

    if(req->req_query)
        NutPrintString(sostream, req->req_query);
    else
        NutPrintString(sostream, "None");
    NutPrintString(sostream, "</BODY>"
                          "</HTML>");
    NutPrintFlush(sostream);

    return 0;
}

THREAD(NutMain, arg)
{
    u_long baud = 115200;
    TCPSOCKET *sock;
    NUTDEVICE *sostream;

    /*
     * Register uart0, the one on the CPU chip.
     * We do not supply base address or interrupt.
     */
    NutRegisterDevice(&devUart0, 0, 0);

    /*
     * Open out uart device.
     */
    uart = NutDeviceOpen("uart0");
    NutDeviceIOCtl(uart, UART_SETSPEED, &baud);
    NutPrintString(uart, "\r\nHTTP Daemon...");
    NutPrintFlush(uart);

    /*
     * Register Realtek controller at address 8300 hex
     * and interrupt 5.
     */
    NutRegisterDevice(&devEth0, 0x8300, 5);

    /*
     * Configure lan interface. Note that we pass
     * IP-address and IP-mask as zero, which will
     * enable DHCP. If you don't got DHCP, call
     * something like
     *
    NutNetIfConfig("eth0", mac, inet_addr("192.168.192.100"), inet_addr("255.255.255.0"));
     */
    NutNetAutoConfig("eth0");
    
    NutPrintString(uart, "ready\r\n");
    NutPrintFlush(uart);
    //for(;;) 
    //  NutThreadYield();

    /*
     * Register our CGI sample. This will be called
     * by http://host/cgi-bin/test.cgi?anyparams
     */
    NutRegisterCgi("test.cgi", ShowQuery);

    /*
     * Protect the cgi-bin directory with
     * user and password.
     */
    NutRegisterAuth("cgi-bin", "root:root");

    /*
     * Now loop endless for connections.
     */
    for(;;) {
        /*
         * Create a socket.
         */
        sock = NutTcpCreateSocket();

        /*
         * Listen on port 80. If we return,
         * we got a client.
         */
        NutTcpAccept(sock, 80);

        /*
         * Create a device from the socket,
         * so we can use NutPrintFormat() and
         * other device goodies.
         */
        sostream = NutSoStreamCreate(sock);

        /*
         * Process http request (name will change soon).
         */
        NutHttpProcessRequest(sostream);

        /*
         * Destroy our device.
         */
        NutSoStreamDestroy(sostream);

        /*
         * Close our socket.
         */
        NutTcpCloseSocket(sock);
    }
}

© 2000-2001 by egnite Software GmbH - visit http://www.ethernut.de/