#include "HTTPPOSTHandler.h"
#include "HTTPParameters.h"
#include "HTTPSession.h"
#include "HTTPServer.h"
#include "HTTPRequest.h"
#include "ZString.h"
#include "IMFFormData.h"
#include "Exception.h"
#include "Log.h"

namespace http {

   int HTTPPOSTHandler::processCommand(coreutils::ZString &request, core::TCPSession &session) {

      HTTPRequest httpRequest(request);

      HTTPSession *httpSession = ((HTTPServer &)(session.server)).httpSessions.findSessionByHeader(httpRequest);

      std::stringstream content;

      HTTPParameters p(httpRequest, session, *httpSession, content, ((HTTPServer &)(session.server)).actionList);

      try {
         ((HTTPServer &)(session.server)).actionList.processRequest(p);
	      httpRequest.response.setCode("200");
	      httpRequest.response.setText("OK");
      }
      catch(coreutils::Exception e) {
	      httpRequest.response.setCode("404");
	      httpRequest.response.setText("Not Found");
      }

      session.out << httpRequest.response.getResponse(content).str();
      coreutils::Log(coreutils::LOG_DEBUG_1) << session.out.str();
      return true;
   }

}
