133 lines
No EOL
4.8 KiB
Text
133 lines
No EOL
4.8 KiB
Text
;********************************
|
|
;*
|
|
;* lweb_http.pbi
|
|
;*
|
|
|
|
Procedure.s http_day(Tag.i)
|
|
Select DayOfWeek(Date())
|
|
Case 0
|
|
ProcedureReturn "Sun, "
|
|
Case 1
|
|
ProcedureReturn "Mon, "
|
|
Case 2
|
|
ProcedureReturn "Tue, "
|
|
Case 3
|
|
ProcedureReturn "Wed, "
|
|
Case 4
|
|
ProcedureReturn "Thur, "
|
|
Case 5
|
|
ProcedureReturn "Fri, "
|
|
Case 6
|
|
ProcedureReturn "Sat, "
|
|
EndSelect
|
|
EndProcedure
|
|
|
|
Procedure.s http_month(Monat.i)
|
|
Select Month(Date())
|
|
Case 1
|
|
ProcedureReturn " Jan "
|
|
Case 2
|
|
ProcedureReturn " Feb "
|
|
Case 3
|
|
ProcedureReturn " Mar "
|
|
Case 4
|
|
ProcedureReturn " Apr "
|
|
Case 5
|
|
ProcedureReturn " May "
|
|
Case 6
|
|
ProcedureReturn " June "
|
|
Case 7
|
|
ProcedureReturn " July "
|
|
Case 8
|
|
ProcedureReturn " Aug "
|
|
Case 9
|
|
ProcedureReturn " Sept "
|
|
Case 10
|
|
ProcedureReturn " Oct "
|
|
Case 11
|
|
ProcedureReturn " Nov "
|
|
Case 12
|
|
ProcedureReturn " Dec "
|
|
EndSelect
|
|
EndProcedure
|
|
|
|
Procedure.s http_header_generate(Map Header.s())
|
|
Protected.s v_http_header, v_current
|
|
|
|
If Not FindMapElement(Header(), #http_head_status) : Header(#http_head_status) = "400 Bad Request" : EndIf
|
|
If Not FindMapElement(Header(), #http_head_date) : Header(#http_head_date) = http_day(DayOfWeek(Date())) +
|
|
Str(Day(Date())) +
|
|
http_month(Month(Date())) +
|
|
Str(Year(Date())) +
|
|
" " +
|
|
FormatDate("%hh:%ii:%ss GMT+1", Date()) : EndIf
|
|
If Not FindMapElement(Header(), #http_head_server) : Header(#http_head_server) = configuration\identifikation : EndIf
|
|
If Not FindMapElement(Header(), #http_head_content_length) : Header(#http_head_content_length) = "0" : EndIf
|
|
If Not FindMapElement(Header(), #http_head_content_type) : Header(#http_head_content_type) = "text/txt" : EndIf
|
|
|
|
v_http_header = "HTTP/1.1 " + Header(#http_head_status) + #CRLF$ +
|
|
#http_head_date + " " + Header(#http_head_date) + #CRLF$ +
|
|
#http_head_server + " " + Header(#http_head_server) + #CRLF$ +
|
|
#http_head_content_length + " " + Header(#http_head_content_length) + #CRLF$ +
|
|
#http_head_content_type + " " + Header(#http_head_content_type) + #CRLF$
|
|
If MapSize(Header()) > 5
|
|
ResetMap(Header())
|
|
While NextMapElement(Header())
|
|
v_current = MapKey(Header())
|
|
If Bool(v_current <> #http_head_status And v_current <> #http_head_date And v_current <> #http_head_server And v_current <> #http_head_content_length And v_current <> #http_head_content_type)
|
|
v_http_header + v_current + " " + Header(v_current) + #CRLF$
|
|
EndIf
|
|
Wend
|
|
EndIf
|
|
v_http_header + #CRLF$
|
|
|
|
ProcedureReturn v_http_header
|
|
EndProcedure
|
|
|
|
Procedure.s Work_Header_to_JSONMap(String.s)
|
|
Protected NewMap Header.s()
|
|
Define.s JSONString, Working, Working_Line
|
|
Define CountLines, Lines, JSON
|
|
If CountString(String,#CRLF$+#CRLF$)
|
|
Working = StringField(String,1,#CRLF$+#CRLF$)
|
|
Else
|
|
ProcedureReturn #error_string
|
|
EndIf
|
|
Lines = CountString(Working,#CRLF$)
|
|
If Lines
|
|
Working_Line = StringField(Working,1,#CRLF$)
|
|
If CountString(Working_Line," ")
|
|
Header(#http_head_method) = UCase(StringField(Working_Line,1," "))
|
|
Header(#http_head_request) = StringField(Working_Line,2," ")
|
|
Header(#http_head_protocol) = UCase(StringField(Working_Line,3," "))
|
|
If CountString(Header(#http_head_request), "?")
|
|
Header(#http_head_query) = StringField(Header(#http_head_request),2,"?")
|
|
Header(#http_head_request) = StringField(Header(#http_head_request),1,"?")
|
|
Else
|
|
Header(#http_head_query) = ""
|
|
EndIf
|
|
For CountLines = 2 To Lines
|
|
Working_Line = StringField(Working,CountLines,#CRLF$)
|
|
Header(LCase(Trim(StringField(Working_Line,1,":")))+":") = Trim(StringField(Working_Line,2,":"))
|
|
Next
|
|
Else
|
|
FreeMap(Header())
|
|
ProcedureReturn #error_string
|
|
EndIf
|
|
Else
|
|
FreeMap(Header())
|
|
ProcedureReturn #error_string
|
|
EndIf
|
|
JSON = CreateJSON(#PB_Any)
|
|
|
|
If JSON
|
|
InsertJSONMap(JSONValue(JSON), Header())
|
|
JSONString = ComposeJSON(JSON)
|
|
FreeMap(Header())
|
|
FreeJSON(JSON)
|
|
ProcedureReturn JSONString.s
|
|
Else
|
|
ProcedureReturn #error_string
|
|
EndIf
|
|
|
|
EndProcedure |