Dependents:   SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1

services/http/server/HttpRequestHandler.h

Committer:
donatien
Date:
2010-05-24
Revision:
0:a2dd0ba6cd2d
Child:
1:7043cc0db03c

File content as of revision 0:a2dd0ba6cd2d:

#ifndef HTTP_REQUEST_HANDLER_H
#define HTTP_REQUEST_HANDLER_H

#include "if/net/net.h"
#include "api/TcpSocket.h"
//#include "HttpServer.h"

#include "mbed.h"

#define HTTP_REQUEST_TIMEOUT 5000

#include <string>
using std::string;

#include <map>
using std::map;

class HttpRequestHandler : public NetService
{
public:
  HttpRequestHandler(const char* rootPath, const char* path, TcpSocket* pTcpSocket);
  virtual ~HttpRequestHandler();

//protected:
  virtual void doGet() = 0;
  virtual void doPost() = 0;
  virtual void doHead() = 0;
  
  virtual void onReadable() = 0; //Data has been read
  virtual void onWriteable() = 0; //Data has been written & buf is free
  virtual void onTimeout(); //Connection has timed out
  virtual void onClose() = 0; //Connection is closing
  
  virtual void close(); //Close socket and destroy data

protected:  
  map<string, string>& reqHeaders() /*const*/;
  string& path() /*const*/;
  int dataLen() const;
  int readData(char* buf, int len);
  string& rootPath() /*const*/;
  
  void setErrCode(int errc);
  void setContentLen(int len);
  
  map<string, string>& respHeaders();
  int writeData(const char* buf, int len);
  
  void setTimeout(int ms);
  void resetTimeout();

private:
  void readHeaders(); //Called at instanciation
  void writeHeaders(); //Called at the first writeData call
  void onTcpSocketEvent(TcpSocketEvent e);
   
  TcpSocket* m_pTcpSocket;
  map<string, string> m_reqHeaders;
  map<string, string> m_respHeaders;
  string m_rootPath;
  string m_path;
  int m_errc; //Response code
  
  Timeout m_watchdog;
  int m_timeout;
  
  bool m_closed;
  bool m_headersSent;
  
  int readLine(char* str, int maxLen);

};

#endif