(********************************************************************)
(*                                                                  *)
(*  gethttps.s7i  Support to get data with the HTTPS protocol       *)
(*  Copyright (C) 2013  Thomas Mertes                               *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library 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 Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser 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 "gethttp.s7i";
include "tls.s7i";


const func file: openHttps (in httpLocation: locationData) is func
  result
    var file: sock is STD_NULL;
  begin
    # writeln("openHttps: " <& literal(locationData.serverName) <& " " <& locationData.portNumber);
    # writeln(locationData.hostName <& ":" <& locationData.portNumber <& "/" <& locationData.path);
    # writeln("params=" <& locationData.params);
    if locationData.httpsProtocol then
      sock := openTlsSocket(locationData.serverName, locationData.portNumber);
    else
      sock := openInetSocket(locationData.serverName, locationData.portNumber);
    end if;
    if sock <> STD_NULL then
      sendGet(sock, locationData);
    end if;
  end func;


const func file: openHttps (in string: serverName, in integer: serverPortNumber,
    in string: location) is func
  result
    var file: sock is STD_NULL;
  local
    var httpLocation: locationData is httpLocation.value;
  begin
    locationData := getHttpLocation(location, httpsDefaultPort);
    locationData.httpsProtocol := TRUE;
    locationData.serverName := serverName;
    locationData.portNumber := serverPortNumber;
    sock := openHttps(locationData);
  end func;


const func file: openHttps (in string: location) is func
  result
    var file: sock is STD_NULL;
  local
    var httpLocation: locationData is httpLocation.value;
  begin
    locationData := getHttpLocation(location, httpsDefaultPort);
    locationData.httpsProtocol := TRUE;
    if proxyServer <> "" then
      locationData.serverName := proxyServer;
      locationData.portNumber := proxyHttpPort;
    end if;
    sock := openHttps(locationData);
  end func;


const func string: getHttps (in string: serverName, in integer: portNumber, in string: location) is func
  result
    var string: data is "";
  local
    var httpLocation: locationData is httpLocation.value;
    var file: sock is STD_NULL;
    var string: statusCode is "";
    var boolean: okay is TRUE;
    var integer: repeatCount is 0;
  begin
    locationData := getHttpLocation(location, httpDefaultPort);
    locationData.httpsProtocol := TRUE;
    locationData.serverName := serverName;
    locationData.portNumber := portNumber;
    repeat
      okay := TRUE;
      sock := openHttps(locationData);
      if sock <> STD_NULL then
        statusCode := getHttpStatusCode(sock);
        # writeln("statusCode: " <& statusCode);
        if statusCode = "301" or statusCode = "302" or
            statusCode = "303" or statusCode = "307" then
          locationData := getHttpLocation(locationData, sock);
          close(sock);
          sock := STD_NULL;
          okay := FALSE;
          incr(repeatCount);
        end if;
      end if;
    until okay or repeatCount > 5;
    if sock <> STD_NULL then
      data := getHttp(sock);
      close(sock);
    end if;
  end func;


(**
 *  Get data specified by a ''location'' using the HTTPS protocol.
 *   getHttps("example.com")
 *   getHttps("www.example.com/index.html")
 *  @param location Url without https:// at the beginning.
 *  @return the string of data found, or "" if nothing was found.
 *)
const func string: getHttps (in string: location) is func
  result
    var string: data is "";
  local
    var httpLocation: locationData is httpLocation.value;
    var file: sock is STD_NULL;
    var string: statusCode is "";
    var boolean: okay is TRUE;
    var integer: repeatCount is 0;
  begin
    locationData := getHttpLocation(location, httpsDefaultPort);
    locationData.httpsProtocol := TRUE;
    if proxyServer <> "" then
      locationData.serverName := proxyServer;
      locationData.portNumber := proxyHttpPort;
    end if;
    repeat
      okay := TRUE;
      sock := openHttps(locationData);
      if sock <> STD_NULL then
        statusCode := getHttpStatusCode(sock);
        # writeln("statusCode: " <& statusCode);
        if statusCode = "301" or statusCode = "302" or
            statusCode = "303" or statusCode = "307" then
          locationData := getHttpLocation(locationData, sock);
          close(sock);
          sock := STD_NULL;
          okay := FALSE;
          incr(repeatCount);
        end if;
      end if;
    until okay or repeatCount > 5;
    if sock <> STD_NULL then
      data := getHttp(sock);
      close(sock);
    end if;
  end func;