/*
 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
 * 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.
 *
 * This file is part of the lwIP TCP/IP stack.
 * 
 * Author: Adam Dunkels <adam@sics.se>
 *
 */

// ========================================================
// Improved httpd for Contrib server example [RAW]
// Aug 10, 2009 : Chris Strahm
// Modified:
//   - conn_err()
//   - close_conn()
//   - http_poll()
//   - http_sent()
//   - send_data()
//   - http_recv()
//   - http_accept()
// ========================================================

#include "lwip/debug.h"
#include "lwip/stats.h"
#include "httpd.h"
#include "lwip/tcp.h"
#include "fs.h"
#include "lwip/err.h"

// Uses these to select if you want debugging output
#define DEBUG_PRINT printf	// enable
//#define DEBUG_PRINT		// disable

const char *tcp_state_str[] = {
  "CLOSED",      
  "LISTEN",      
  "SYN_SENT",    
  "SYN_RCVD",    
  "ESTABLISHED", 
  "FIN_WAIT_1",  
  "FIN_WAIT_2",  
  "CLOSE_WAIT",  
  "CLOSING",     
  "LAST_ACK",    
  "TIME_WAIT"   
};


struct http_state {
  char *file;
  u16_t left;
  u8_t retries;
};


/*-----------------------------------------------------------------------------------*/
static void conn_err(void *arg, err_t err) {
  struct http_state *hs=arg;
  LWIP_UNUSED_ARG(err);
  DEBUG_PRINT("conn_err: %s\n", lwip_strerr(err));
  if(hs) mem_free(hs);
}


/*-----------------------------------------------------------------------------------*/
static void close_conn(struct tcp_pcb *pcb, struct http_state *hs) {
  err_t err;
  if(hs) DEBUG_PRINT("close_conn: pcb=0x%08X hs=0x%08X left=%d\n", pcb, hs, hs->left); 
	else DEBUG_PRINT("close_conn: pcb=0x%08X hs=0x%08X\n ", pcb, hs);
  tcp_arg (pcb, NULL);
  tcp_sent(pcb, NULL);
  tcp_recv(pcb, NULL);
  if(hs) mem_free(hs);
  err = tcp_close(pcb);
  if(err != ERR_OK)
  	printf("Error %s closing pcb=0x%08X\n", lwip_strerr(err), pcb);
}


/*-----------------------------------------------------------------------------------*/
static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
  struct http_state *hs=arg;
  LWIP_UNUSED_ARG(len);
  DEBUG_PRINT("http_sent: pcb=0x%08X hs=0x%08X len=%d\n", pcb, hs, len);
  if(hs==NULL) return (ERR_OK); // nothing to do if hs has been free'd
  hs->retries = 0;				// reset retry counter
  tcp_sent (pcb, NULL);		    // Temporarily disable send notifications 
  send_data(pcb, hs);			// send more data 
  tcp_sent (pcb, http_sent);    // Reenable notifications 
  return (ERR_OK);
}


/*-----------------------------------------------------------------------------------*/
static err_t http_poll(void *arg, struct tcp_pcb *pcb) {
  struct http_state *hs=arg;
  if(pcb) DEBUG_PRINT("http_poll: pcb=0x%08X hs=0x%08X pcb_state=%s\n", pcb, hs, tcp_state_str[pcb->state]);
    else DEBUG_PRINT("http_poll: pcb=0x%08X hs=0x%08X \n", pcb, hs);
  if (hs == NULL) { // hs free'd, abort conn
    tcp_abort(pcb);
    return (ERR_ABRT);
  	} else {
    	++hs->retries;
    	if (hs->retries == 4) {  // too many retry, abort conn
      		tcp_abort(pcb);
      		return (ERR_ABRT);
    		}
    	send_data(pcb, hs);		// try sending more data
  		}
  return (ERR_OK);
}


/*-----------------------------------------------------------------------------------*/
static void send_data(struct tcp_pcb *pcb, struct http_state *hs) {
  err_t err;
  u16_t len;

  if(hs) DEBUG_PRINT("send_data: pcb=0x%08X hs=0x%08X left=%d\n", pcb, hs, hs->left);
    else DEBUG_PRINT("send_data: pcb=0x%08X hs=0x%08X\n", pcb, hs);

  // hs was free'd, nothing to do 
  if(!hs) return;

  if(hs->left==0) {
      DEBUG_PRINT("send_data: EndOfFile closing\n");
	  close_conn(pcb, hs); 	// all data queued, time to close
	  return;
    }
    
  // We cannot send more data than space available in the send buffer      
  if (tcp_sndbuf(pcb) < hs->left) {
    len = tcp_sndbuf(pcb);
    } else len = hs->left;

  if(len > (2*pcb->mss)) len = 2*pcb->mss; // limit to maximum also

  do {
    err = tcp_write(pcb, hs->file, len, 0);
    if (err == ERR_MEM) len /= 2;
    } while (err == ERR_MEM && len > 1);  
  
  if (err == ERR_OK) {
    hs->file += len;
    hs->left -= len;
    // we have queued more data, send it
    DEBUG_PRINT("send_data: tcp_output\n");
    tcp_output(pcb);
    }
}


/*-----------------------------------------------------------------------------------*/
static err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
  int i;
  char *data;
  struct fs_file file;
  struct http_state *hs=arg;

  DEBUG_PRINT("http_recv: pcb=0x%08X pbuf=0x%08X err=%s\n", pcb, p, lwip_strerr(err));

  // handle case of hs mem alloc failed during accept
  if (hs==NULL) {  
	    printf("Error, http_recv: hs was Null!\n");
		tcp_abort(pcb);
		return (ERR_ABRT);
		}

  // handle pbuf=null
  if (err == ERR_OK) {
    if (p == NULL) {
      if (pcb->state == CLOSE_WAIT) {
        tcp_close(pcb); 				// received FIN, need to send FIN
	    return (ERR_OK);
        }
      close_conn(pcb, hs);				// close connection
 	  return (ERR_OK);
      } 
	}


  if (err == ERR_OK) {
    // Inform TCP that we have taken the data. 
    tcp_recved(pcb, p->tot_len);
    if (hs->file == NULL) {
      data = p->payload;
      
      if (strncmp(data, "GET ", 4) == 0) {
        for(i = 0; i < 40; i++) {
          if (((char *)data + 4)[i] == ' ' ||
             ((char *)data + 4)[i] == '\r' ||
             ((char *)data + 4)[i] == '\n') {
             ((char *)data + 4)[i] = 0;
             }
          }

        if (*(char *)(data + 4) == '/' &&
           *(char *)(data + 5) == 0) {
           fs_open("/index.html", &file);
           } else if (!fs_open((char *)data + 4, &file)) {
                    fs_open("/404.html", &file);
                    }

        hs->file = file.data;
        hs->left = file.len;
        DEBUG_PRINT("data %p len %ld\n", hs->file, hs->left);

        pbuf_free(p);

        // Tell TCP that we wish be to informed of data that has been
        // successfully sent by a call to the http_sent() function. 
        tcp_sent(pcb, http_sent);

        // Start sending data 
        send_data(pcb, hs);

      } else { // non GET
        pbuf_free(p);
        close_conn(pcb, hs);
        }
    } // hs->file == null 
    else { 
      pbuf_free(p);
      }
    } // if err=ERR_OK
  return (ERR_OK);
} // http_recv


/*-----------------------------------------------------------------------------------*/
static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
  struct http_state *hs;
  LWIP_UNUSED_ARG(arg);
  LWIP_UNUSED_ARG(err);
  tcp_setprio(pcb, TCP_PRIO_MIN);
//  tcp_setprio(pcb, TCP_PRIO_MAX);		// may be faster? 

  // Allocate memory for the structure that holds the state of the connection. 
  hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
  if (hs == NULL) {
    printf("http_accept: Out of memory\n");
    return ERR_MEM;
    }
  
  // Initialize the structure. 
  hs->file = NULL;
  hs->left = 0;
  hs->retries = 0;
  
  // Tell TCP that this is the structure we wish to be passed for our callbacks. 
  tcp_arg(pcb, hs);
  // Tell TCP that we wish to be informed of incoming data by a call to the http_recv() function. 
  tcp_recv(pcb, http_recv);
  tcp_err(pcb, conn_err);
  tcp_poll(pcb, http_poll, 4);
  return (ERR_OK);
}


/*-----------------------------------------------------------------------------------*/
void httpd_init(void) {
  struct tcp_pcb *pcb;

  pcb = tcp_new();
  tcp_bind(pcb, IP_ADDR_ANY, 80);
  pcb = tcp_listen(pcb);
  tcp_accept(pcb, http_accept);
}

/*-----------------------------------------------------------------------------------*/

