#include "__jet.h"
#include "Exception.h"
#include "Global.h"
#include "SessionId.h"
#include <iostream>
#include <fstream>

namespace jet {
   
   __jet::__jet(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, this) {
      char *cookies;      
      if(keywordDefined("cgi") && (resolveKeyword("cgi") == "true")) {
	 global.cgi = true;
	 
	 if(keywordDefined("sessiondir")) {
	    global.session = true;
	    global.cookies = getenv("HTTP_COOKIE");
	    
//	    if request_has_cookie then
//	      pull sessionfile from sessiondir.
//	      if last activity time is expired then ignore.
//	      follow sessiontimeoutredirecturl.
//	    else
	    SessionId sessionId;
	    global.headers["Set-Cookie"] << "session=" << sessionId;
	    if(keywordDefined("sessiontimeout")) {
	       time_t timeout = time(0) + keywords["sessiontimeout"].asInteger();
	    }
	    
//                 also save last activity time in session file.
	    
	 }
	 	 
	 coreutils::ZString requestMethod(getenv("REQUEST_METHOD"));
	 if(requestMethod == "POST") {
	    coreutils::ZString contentLength(getenv("CONTENT_LENGTH"));
	    coreutils::ZString contentType(getenv("CONTENT_TYPE"));
	    
	    if(contentType == "multipart/form-data") 	 
	       coreutils::IMFMultipart postdata(0, "");	      
	    
//	    else if(contentType == "application/x-www-form-urlencoded") 
//	      global.setupFormURLEncoded(postdata);
	 }	
      }
      processContainer(container, NULL, true);
   }
}
