Redirect on Directory to subdirectory with default index file

This commit is contained in:
René Linder 2022-02-18 16:05:48 +01:00
parent 3a7183a391
commit 8e335d7da1
2 changed files with 107 additions and 71 deletions

View file

@ -74,6 +74,7 @@ Module lhs_web
defaultfile.s
basedir.s
error400.s
errorfile404.s
type.i
EndStructure
;}
@ -94,6 +95,7 @@ Module lhs_web
configuration\defaultfile = "index.html"
configuration\basedir = "/srv/lweb-srv/"
configuration\error400 = "integrated" ; Kann "integrated" sein wass die integrierte Standard Fehlermeldung hervorruft.
configuration\errorfile404 = "error.html"
configuration\type = 0
configuration\http\max_clients = 10 ; Max sametime HTTP connections.
@ -243,7 +245,7 @@ Module lhs_web
Declare server_HTTP(network_server_id.i)
Declare server_HTTPS(network_server_id.i)
Declare client(network_client_id.i)
Declare.s file_check(thread_requested.s)
Declare call_request(RequestString.s, Info.i=#get_handler_procedure)
Declare.s Work_Post_ToJSON_multipart_form_data(ContentLength.i, MemorSize.i, Memory.i)
@ -495,7 +497,7 @@ Module lhs_web
Protected.s thread_requested, thread_type, thread_date, thread_header, thread_work, JSONStringToMap, Handler_Response, response_status, PostMapString
Protected thread_buffer, thread_buffer_offset, thread_buffer_length, buffer_sent
Protected sent_length, sent_buffer_address, sent_total
Protected.b thread_alive = #True, error_message = #False
Protected.b thread_alive = #True, error_message = #False, thread_redirect = #False
Define NewMap Header.s()
Define NewMap Response.s()
Define NewMap Post.s()
@ -629,11 +631,13 @@ Module lhs_web
;*
;{
lhs_log::Out(#http_method_get)
If Header(#http_head_request) = "/"
thread_requested = configuration\defaultfile
Else
thread_requested = Header(#http_head_request)
EndIf
lhs_log::Out("Requested:"+thread_requested)
ToCallType = call_request(thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_get
@ -641,6 +645,13 @@ Module lhs_web
Else
ToCall = 0
EndIf
If ToCall = 0
If Right(thread_requested,1) = "/"
thread_requested = thread_requested+configuration\defaultfile
Else
thread_requested = thread_requested
EndIf
EndIf
If ToCall > 0 ;Dann ist eine Funktion hinterlegt und zulässig aufgerufen zu werden.
;{ Dynamischer WebHandler
@ -718,15 +729,16 @@ Module lhs_web
Else
;{ Uncached file sems to be stable tested up to 200 clients and 100 requests.
If file_check(thread_requested) = thread_requested
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead)
If Not thread_file_handle
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead)
lhs_log::Out("FileDir:" + configuration\basedir + configuration\defaultfile)
If Not thread_file_handle
lhs_log::Out("Error file set")
thread_file_handle = ReadFile(#PB_Any, "error.html")
EndIf
EndIf
thread_data_size = Lof(thread_file_handle)
; Is File bigger than MaxFileSize in Memory allowed ?
If thread_data_size >= configuration\mem\MaxFileSize
@ -742,6 +754,9 @@ Module lhs_web
CloseFile(thread_file_handle)
thread_oversized_file = #False
EndIf
Else
thread_redirect = #True
EndIf
;}
@ -873,11 +888,13 @@ Module lhs_web
Else
;{ Uncached file sems to be stable tested up to 200 clients and 100 requests.
If file_check(thread_requested) = thread_requested
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead)
If Not thread_file_handle
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead)
lhs_log::Out("FileDir:" + configuration\basedir + configuration\defaultfile)
If Not thread_file_handle
lhs_log::Out("Error file set")
thread_file_handle = ReadFile(#PB_Any, "error.html")
EndIf
EndIf
@ -897,25 +914,15 @@ Module lhs_web
CloseFile(thread_file_handle)
thread_oversized_file = 0
EndIf
Else
;Send Redirect:
thread_redirect = #True
EndIf
;}
EndIf
If Header(#http_head_request) = "/"
thread_requested = configuration\defaultfile
Else
thread_requested = Header(#http_head_request)
EndIf
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead)
If Not thread_file_handle
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead)
lhs_log::Out("FileDir:" + configuration\basedir + thread_requested)
If Not thread_file_handle
thread_file_handle = ReadFile(#PB_Any, "error.html")
EndIf
EndIf
Default
@ -926,7 +933,7 @@ Module lhs_web
;* Read Buffer to Memory and Clear Buffer to Zero until the complete Networkbuffer from this Client is Cleaned.
;*
EndSelect
If thread_data_size
If thread_data_size Or thread_redirect = #True
If thread_type = ""
thread_type = mimetype(GetExtensionPart(thread_requested))
EndIf
@ -949,14 +956,21 @@ Module lhs_web
If Response(#http_head_set_cookie) <> ""
Header(#http_head_set_cookie) = Response(#http_head_set_cookie)
EndIf
If thread_redirect = #True
Header(#http_head_redirect) = file_check(thread_requested)
Header(#http_head_status) = "303 See Other"
Else
Header(#http_head_content_length) = Str(thread_data_size)
Header(#http_head_content_type) = thread_type
EndIf
Header(#http_head_connection) = "Keep-Alive"
Header(#http_head_keep_alive) = "timeout=15, max=1000"
thread_header = http_header_generate(Header())
;large File Handling
If thread_oversized_file = #False
If thread_redirect = #True
thread_buffer = AllocateMemory(StringByteLength(thread_header)+12)
ElseIf thread_oversized_file = #False
thread_buffer = AllocateMemory(thread_data_size+StringByteLength(thread_header)+12)
Else
thread_buffer = AllocateMemory(thread_temp_file_readed+StringByteLength(thread_header)+12)
@ -967,16 +981,18 @@ Module lhs_web
lhs_log::Out("Header Finished")
EndIf
;Copy temporary File Buffer to normal Buffer.
If thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0 And thread_oversized_file = #False
If thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0 And thread_oversized_file = #False And thread_redirect = #False
CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_data_size)
lhs_log::Out("Cache Address Memory:"+Str(thread_temp_cache_memory))
FreeMemory(thread_temp_cache_memory)
thread_temp_cache_memory = 0
ElseIf thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0 And thread_temp_file_readed <> 0 And thread_oversized_file = #True
ElseIf thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0 And thread_temp_file_readed <> 0 And thread_oversized_file = #True And thread_redirect = #False
CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_temp_file_readed)
lhs_log::Out("Cache Address Memory:"+Str(thread_temp_cache_memory))
FreeMemory(thread_temp_cache_memory)
thread_temp_cache_memory = 0
ElseIf thread_redirect = #True
thread_redirect = #False
Else
lhs_log::Out("File Buffer Troubles.")
If thread_temp_cache_memory = 0 : lhs_log::Out("thread_temp_cache_memory = 0") : EndIf
@ -996,7 +1012,9 @@ Module lhs_web
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
;Send the data in memory to client.
If thread_oversized_file = #False
If thread_redirect = #True
sent_total = thread_buffer_offset - thread_buffer
ElseIf thread_oversized_file = #False
sent_total = thread_data_size+(thread_buffer_offset-thread_buffer)
Else
sent_total = thread_temp_file_readed+(thread_buffer_offset-thread_buffer)
@ -1271,6 +1289,23 @@ Module lhs_web
EndSelect
EndProcedure
Procedure.s file_check(thread_requested.s)
Protected file_type.i
file_type = FileSize(configuration\basedir + thread_requested)
If file_type = -1
thread_requested = configuration\errorfile404
ElseIf file_type = -2
;Is a Directory
If Right(thread_requested,1) = "/"
thread_requested = thread_requested + configuration\defaultfile
Else
thread_requested = thread_requested + "/" + configuration\defaultfile
EndIf
EndIf
ProcedureReturn thread_requested.s
EndProcedure
EndModule

View file

@ -97,6 +97,7 @@ DeclareModule lhs_web
#http_head_keep_alive = "keep-alive:"
#http_head_cookie = "cookie:"
#http_head_set_cookie = "set-cookie:"
#http_head_redirect = "location:"
#http_content_type_application_x_www_form_urlencoded = "application/x-www-form-urlencoded"
#http_content_type_multipart_form_data = "multipart/form-data"