(********************************************************************)
(*                                                                  *)
(*  comanche.sd7  Simple webserver for static and cgi pages.        *)
(*  Copyright (C) 2009 - 2015  Thomas Mertes                        *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


$ include "seed7_05.s7i";
  include "httpserv.s7i";
  include "x509cert.s7i";
  include "osfiles.s7i";
  include "shell.s7i";
  include "getf.s7i";

var string: htdocs is "../htdocs";
var string: cgi_bin is "../prg";


const proc: sendHttpResponse (inout file: sock, in string: content,
    in var string: contentType, in array string: header) is func
  local
    var string: line is "";
    var string: status is "";
    var string: response is "";
  begin
    # writeln("sendHttpResponse: len=" <& length(content) <& " " <& contentType);
    for line range header do
      if startsWith(line, "Status") then
        status := trim(line[succ(pos(line, ":")) ..]);
      elsif startsWith(line, "Content-Type") then
        contentType := trim(line[succ(pos(line, ":")) ..]);
      end if;
    end for;
    if status = "" then
      response &:= "HTTP/1.1 200 OK\r\n";
    else
      response &:= "HTTP/1.1 " <& status <& "\r\n";
    end if;
    response &:= "Server: Comanche\r\n";
    # response &:= "Transfer-Encoding: identity\r\n";
    response &:= "Cache-Control: max-age=259200\r\n";
    if contentType <> "" then
      response &:= "Content-Type: " <& contentType <& "\r\n";
    elsif startsWith(content, "\137;PNG") then
      response &:= "Content-Type: image/png\r\n";
    elsif pos(content, "<html") = 0 then
      response &:= "Content-Type: text/html\r\n";
    else
      response &:= "Content-Type: text/plain\r\n";
    end if;
    # writeln("Content-Length: " <& length(content));
    response &:= "Content-Length: " <& length(content) <& "\r\n";
    for line range header do
      if not startsWith(line, "Content-Type") and
          not startsWith(line, "Connection") then
        response &:= line <& "\r\n";
      end if;
    end for;
    response &:= "Connection: keep-alive\r\n";
    response &:= "\r\n";
    response &:= content;
    block
      write(sock, response);
    exception
      catch FILE_ERROR: close(sock);
    end block;
  end func;


const proc: sendClientError (inout file: sock, in integer: statuscode,
    in string: message, in string: explanation) is func
  local
    var string: response is "";
    var string: htmlMessage is "";
  begin
    htmlMessage := "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\
                   \<html><head>\n\
                   \<title>" <& statuscode <& " " <& message <& "</title>\n\
                   \</head><body>\n\
                   \<h1>" <& message <& "</h1>\n\
                   \<p>" <& explanation <& "</p>\n\
                   \<hr>\n\
                   \<address>Comanche</address>\n\
                   \</body></html>\n";
    response &:= "HTTP/1.1 " <& statuscode <& " " <& message <& "\r\n";
    response &:= "Server: Comanche\r\n";
    response &:= "Transfer-Encoding: identity\r\n";
    response &:= "Content-Length: " <& length(htmlMessage) <& "\r\n";
    response &:= "Content-Type: text/html\r\n";
    response &:= "\r\n";
    response &:= htmlMessage;
    write(sock, response);
  end func;


const func string: callCgi (in string: filePath, in string: queryParams,
    in string: postParams, in string: cookies, inout array string: header) is func
  result
    var string: response is "";
  local
    var string: cgiPath is "";
    var string: tempName is "";
    var file: cgiInput is STD_NULL;
    var file: cgiOutput is STD_NULL;
    var string: line is "";
  begin
    # writeln("CGI " <& filePath <& " " <& queryParams);
    # writeln(postParams);
    cgiPath := toStdPath(cgi_bin & filePath[9 ..]);
    setenv("QUERY_STRING", queryParams);
    setenv("HTTP_COOKIE", cookies);
    tempName := "cgiInput_" & str(rand(0, 99999));
    cgiInput := open(tempName, "w");
    writeln(cgiInput, postParams);
    # writeln(cgiInput, "*");
    # writeln(cgiInput, "*");
    close(cgiInput);
    if endsWith(cgiPath, ".sd7") then
      cgiOutput := popen("./s7 -q " <& cgiPath & " < " & tempName, "r");
    else
      cgiOutput := popen(cgiPath & " < " & tempName, "r");
    end if;
    if cgiOutput <> STD_NULL then
      line := getln(cgiOutput);
      while line <> "" do
        # writeln(line);
        header &:= [] (line);
        line := getln(cgiOutput);
      end while;
      # writeln;
      response := gets(cgiOutput, 999999999);
      # writeln(length(response));
      # writeln(literal(response));
      close(cgiOutput);
      removeFile(tempName);
    end if;
    if response = "" then
      header &:= "Status: 404 Not Found";
      header &:= "Content-Type: text/html";
      response := "<html><head>\n\
                  \<title>CGI Error</title>\n\
                  \</head><body>\n\
                  \<h1>CGI Error</h1>\n\
                  \<p>The requested CGI script " <& filePath <&
                  " could not be executed or did not produce any output.</p>\n\
                  \<p>Query params: " <& queryParams <& "</p>\n\
                  \<p>Post params: " <& postParams <& "</p>\n\
                  \<hr>\n\
                  \<address>Comanche</address>\n\
                  \</body></html>\n";
    end if;
  end func;


const proc: processGet (inout httpRequest: request) is func
  local
    var string: filePath is "";
    var string: cookies is "";
    var array string: cgiHeader is 0 times "";
    var string: buffer is "";
    var string: contentType is "";
  begin
    # writeln("GET " <& request.path);
    if "Cookie" in request.properties then
      cookies := request.properties["Cookie"];
    end if;
    if startsWith(request.path, "/cgi-bin/") then
      buffer := callCgi(request.path, request.queryStri, "", cookies, cgiHeader);
      if buffer <> "" then
        sendHttpResponse(request.sock, buffer, "", cgiHeader);
      end if;
    else
      filePath := toStdPath(htdocs & "/" & request.path);
      # writeln(literal(filePath));
      if fileType(filePath) = FILE_ABSENT then
        if fileType(filePath & ".html") <> FILE_ABSENT then
          filePath &:= ".html";
        elsif fileType(filePath & ".htm") <> FILE_ABSENT then
          filePath &:= ".htm";
        end if;
      elsif fileType(filePath) = FILE_DIR then
        if fileType(filePath & "/index.html") <> FILE_ABSENT then
          filePath &:= "/index.html";
        elsif fileType(filePath & "/index.htm") <> FILE_ABSENT then
          filePath &:= "/index.htm";
        end if;
      end if;
      if fileType(filePath) = FILE_REGULAR then
        buffer := getf(filePath);
      else
        buffer := "";
      end if;
      if buffer <> "" then
        if endsWith(filePath, ".htm") or endsWith(filePath, ".html") then
          contentType := "text/html";
        end if;
        sendHttpResponse(request.sock, buffer, contentType, 0 times "");
      end if;
    end if;
    if buffer = "" then
      sendClientError(request.sock, 404, "Not Found",
          "The requested URL " <& request.path <&
          " was not found on this server.");
    end if;
  end func;


const proc: processPost (inout httpRequest: request) is func
  local
    var string: cookies is "";
    var array string: cgiHeader is 0 times "";
    var string: buffer is "";
  begin
    # writeln("POST " <& request.path);
    # writeln("queryStri: " <& request.queryStri);
    # writeln("postParams: " <& request.body);
    if "Cookie" in request.properties then
      cookies := request.properties["Cookie"];
    end if;
    if startsWith(request.path, "/cgi-bin/") then
      buffer := callCgi(request.path, request.queryStri, request.body, cookies, cgiHeader);
    end if;
    if buffer <> "" then
      sendHttpResponse(request.sock, buffer, "", cgiHeader);
    else
      sendClientError(request.sock, 404, "Not Found",
          "The requested URL " <& request.path <&
          " was not found on this server.");
    end if;
  end func;


const proc: main is func
  local
    var array string: args is 0 times "";
    var integer: index is 0;
    var boolean: writeHelp is FALSE;
    var boolean: useTls is FALSE;
    var boolean: htdocsAssigned is FALSE;
    var boolean: cgiBinAssigned is FALSE;
    var boolean: okay is TRUE;
    var integer: port is 1080;
    var httpServer: server is httpServer.value;
    var httpRequest: request is httpRequest.value;
  begin
    writeln("Comanche Version 2.0 - Simple webserver for static and cgi pages");
    writeln("Copyright (C) 2009 - 2015 Thomas Mertes");
    writeln("This is free software; see the source for copying conditions.  There is NO");
    writeln("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
    writeln("Comanche is written in the Seed7 programming language");
    writeln("Homepage: http://seed7.sourceforge.net");
    args := argv(PROGRAM);
    for key index range argv(PROGRAM) do
      if args[index] in {"-h", "-?"} then
        writeHelp := TRUE;
      elsif args[index] = "-p" and index < length(args) then
        block
          port := integer parse (args[succ(index)]);
        exception
          catch RANGE_ERROR:
            writeln(" ***** Port not numeric. Port " <& port <& " used instead.");
        end block;
        incr(index);
      elsif args[index] = "-tls" then
        useTls := TRUE;
      elsif not htdocsAssigned then
        htdocs := convDosPath(args[index]);
        htdocsAssigned := TRUE;
      elsif not cgiBinAssigned then
        cgi_bin := convDosPath(args[index]);
        cgiBinAssigned := TRUE;
      else
        writeln(" ***** Unrecognized parameter " <& args[index] <& ".");
      end if;
    end for;
    if writeHelp then
      writeln;
      writeln("usage: comanche [-h | -?] [-p port] [-tls] [html-directory [cgi-directory]]");
      writeln;
      writeln("The html-directory specifies the root directory for HTML files.");
      writeln("The default html-directory is \"../htdocs\".");
      writeln("The cgi-directory specifies the root directory for CGI scripts.");
      writeln("The default cgi-directory is \"../prg\".");
      writeln("The default port is 1080.");
    else
      if fileType(htdocs) <> FILE_DIR then
        writeln(" *** Directory " <& literal(htdocs) <& " not found.");
        writeln("     You need to specify a directory which contains");
        writeln("     at least \"index.htm\" and possibly other HTML files.");
        okay := FALSE;
      end if;
      if fileType(cgi_bin) <> FILE_DIR then
        writeln(" *** Directory " <& literal(cgi_bin) <& " not found.");
        writeln("     You need to specify a directory which contains");
        writeln("     executable CGI scripts.");
        okay := FALSE;
      end if;
      if not okay then
        writeln("Use the option -? (or -h) to get more information.");
      else
        writeln("HTML directory: " <& htdocs);
        writeln("CGI directory: " <& cgi_bin);
        writeln("Port: " <& port);
        server := openHttpServer(port, stdCertificate, useTls);
        writeln("To test comanche make sure that " <&
                literal(htdocs & "/index.htm") <& " exists and");
        write("open http");
        if useTls then
          write("s");
        end if;
        writeln("://localhost:" <& port <& "/ in your browser. \
                \To stop comanche press CTRL-C.");
        while TRUE do
          request := getRequest(server);
          # writeln(request.method <& " " <& request.path);
          if request.method = "GET" then
            processGet(request);
          elsif request.method = "POST" then
            processPost(request);
          else
            sendClientError(request.sock, 405, "Method Not Allowed",
                "The HTTP-method " <& request.method <& " is not allowed.");
          end if;
          # process(request);
        end while;
      end if;
    end if;
  end func;