' Gambas module file

Export 

'' Return a collection of all environment variables sent to the CGI script.
''
'' The possible changes done with the [array accessor](../put) are taken into account.
Property Read All As Collection

Private $cCache As New Collection
Private $cKey As New Collection

Private $aDay As String[] 
Private $aMonth As String[] 

Public Sub FormatDate({Date} As Date) As String

  Dim dDate As Date
  
  dDate = {Date} - Frac(Date(Now)) ' Go to GMT

  If Not $aDay Then 
    $aDay = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    $aMonth = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  Endif
  
  Return $aDay[WeekDay(dDate)] & ", " & Format(Day(dDate), "00") & " " & $aMonth[Month(dDate) - 1] & " " & Year(dDate) & " " & Format(Time(dDate), "hh:nn:ss") & " GMT"
  
End

Public Function _get(Key As String) As String

  Dim sVal As String

  sVal = $cCache[Key]
  If Not sVal And If Not $cKey.Exist(Key) Then
    sVal = Application.Env[Key]
    $cCache[Key] = sVal
  Endif
  Return sVal

End

Public Sub _put(Value As String, Key As String)

  $cCache[Key] = Value
  $cKey[Key] = True

End

'' Reset the CGI environment variables decoding, all possible changes to them, and call the [Request.Reset](../../request/reset) method.

Public Sub Reset()
  
  $cKey.Clear
  $cCache.Clear
  Request.Reset
  
End

Public Sub Encode(Path As String) As String
  
  Return URL.Encode(Path)
  
End

Public Sub Decode(Path As String) As String
  
  Return URL.Decode(Path)
  
End

Public Sub _init()
  
  Dim sRoot As String
  Dim sPath As String
  
  'DIM sLang AS String
  'sLang = Trim(Split(_get("HTTP_ACCEPT_LANGUAGE"), ",")[0])
  'IF System.Charset = "UTF-8" THEN sLang &= ".UTF-8"
  'System.Language = sLang
  
  ' Compatibility with Gambas <= 3.6
  Class.Load("URL")
  Component.Load("gb.util.web")
  
  File.In.EndOfLine = gb.Windows
  File.Out.EndOfLine = gb.Windows
  
  sRoot = CGI["SCRIPT_NAME"]
  
  If CGI["SERVER_SOFTWARE"] Begins "thttpd/2.25b" Then
    If sRoot Ends "/" Then
      sRoot = Left$(sRoot, -1)
      sPath = CGI["PATH_INFO"]
      If sRoot Ends sPath Then
        sRoot = Left(sRoot, Len(sRoot) - Len(sPath))
      Endif
    Endif
  Endif
  
  If sRoot Ends "/" Then sRoot = Left$(sRoot, -1)
  If Not sRoot Then sRoot = "/."
  CGI["SCRIPT_NAME"] = sRoot
  
End

'' Send a dump of the CGI environment variables as request response.
''
'' This dump is an HTML table.

Public Sub Dump()
  
  Dim sName As String
  
  Output To Default

  Response.Buffered = False
  Response.Begin
  Print "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"></head><body>"
  Print "<h1>CGI environment</h1>"
  Print "<table border=\"1\" cellpadding=\"4\" cellspacing=\"0\"><tr><th>Name</th><th>Value</th></tr>"
  For Each sName In Application.Env
    Print "<tr><td><tt>"; Html(sName); "</tt></td><td><tt>"; Html(CGI[sName]); "</tt></td></tr>"
  Next
  Print "</table></body>"
  Response.End
  Quit 
  
End


Public Sub Error(Text As String)
  
  Output To Default

  Response.Buffered = False
  Response.Begin
  Print "<!DOCTYPE HTML><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"></head><body>"
  Print "<pre>"; File.Load("error.txt")
  Print Text; "</pre></body>"
  Response.End
  Quit 
  
End

Private Function All_Read() As Collection

  Dim cAll As New Collection
  Dim sKey As String

  For Each sKey In Env
    cAll[sKey] = Env[sKey]
  Next
  
  For Each $cKey
    sKey = $cKey.Key
    cAll[sKey] = $cCache[sKey]
  Next

  Return cAll

End
