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

173
lweb.pbi
View file

@ -74,6 +74,7 @@ Module lhs_web
defaultfile.s defaultfile.s
basedir.s basedir.s
error400.s error400.s
errorfile404.s
type.i type.i
EndStructure EndStructure
;} ;}
@ -94,6 +95,7 @@ Module lhs_web
configuration\defaultfile = "index.html" configuration\defaultfile = "index.html"
configuration\basedir = "/srv/lweb-srv/" configuration\basedir = "/srv/lweb-srv/"
configuration\error400 = "integrated" ; Kann "integrated" sein wass die integrierte Standard Fehlermeldung hervorruft. configuration\error400 = "integrated" ; Kann "integrated" sein wass die integrierte Standard Fehlermeldung hervorruft.
configuration\errorfile404 = "error.html"
configuration\type = 0 configuration\type = 0
configuration\http\max_clients = 10 ; Max sametime HTTP connections. 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_HTTP(network_server_id.i)
Declare server_HTTPS(network_server_id.i) Declare server_HTTPS(network_server_id.i)
Declare client(network_client_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 call_request(RequestString.s, Info.i=#get_handler_procedure)
Declare.s Work_Post_ToJSON_multipart_form_data(ContentLength.i, MemorSize.i, Memory.i) 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.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 thread_buffer, thread_buffer_offset, thread_buffer_length, buffer_sent
Protected sent_length, sent_buffer_address, sent_total 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 Header.s()
Define NewMap Response.s() Define NewMap Response.s()
Define NewMap Post.s() Define NewMap Post.s()
@ -629,11 +631,13 @@ Module lhs_web
;* ;*
;{ ;{
lhs_log::Out(#http_method_get) lhs_log::Out(#http_method_get)
If Header(#http_head_request) = "/" If Header(#http_head_request) = "/"
thread_requested = configuration\defaultfile thread_requested = configuration\defaultfile
Else Else
thread_requested = Header(#http_head_request) thread_requested = Header(#http_head_request)
EndIf EndIf
lhs_log::Out("Requested:"+thread_requested) lhs_log::Out("Requested:"+thread_requested)
ToCallType = call_request(thread_requested, #get_handler_prototype) ToCallType = call_request(thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_get If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_get
@ -641,6 +645,13 @@ Module lhs_web
Else Else
ToCall = 0 ToCall = 0
EndIf 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. If ToCall > 0 ;Dann ist eine Funktion hinterlegt und zulässig aufgerufen zu werden.
;{ Dynamischer WebHandler ;{ Dynamischer WebHandler
@ -718,29 +729,33 @@ Module lhs_web
Else Else
;{ Uncached file sems to be stable tested up to 200 clients and 100 requests. ;{ Uncached file sems to be stable tested up to 200 clients and 100 requests.
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead) If file_check(thread_requested) = thread_requested
If Not thread_file_handle thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead)
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead) If Not thread_file_handle
lhs_log::Out("FileDir:" + configuration\basedir + configuration\defaultfile) thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead)
If Not thread_file_handle lhs_log::Out("FileDir:" + configuration\basedir + configuration\defaultfile)
thread_file_handle = ReadFile(#PB_Any, "error.html") If Not thread_file_handle
EndIf lhs_log::Out("Error file set")
EndIf thread_file_handle = ReadFile(#PB_Any, "error.html")
EndIf
thread_data_size = Lof(thread_file_handle) EndIf
; Is File bigger than MaxFileSize in Memory allowed ? thread_data_size = Lof(thread_file_handle)
If thread_data_size >= configuration\mem\MaxFileSize ; Is File bigger than MaxFileSize in Memory allowed ?
;Do Handle the File another way. If thread_data_size >= configuration\mem\MaxFileSize
thread_temp_cache_memory = AllocateMemory(configuration\mem\MaxFileSize) ;Do Handle the File another way.
thread_temp_file_readed = configuration\mem\DefaultBlockSize thread_temp_cache_memory = AllocateMemory(configuration\mem\MaxFileSize)
thread_data_readed = ReadData(thread_file_handle, thread_temp_cache_memory, thread_temp_file_readed) thread_temp_file_readed = configuration\mem\DefaultBlockSize
thread_data_to_read = thread_data_size - thread_data_readed thread_data_readed = ReadData(thread_file_handle, thread_temp_cache_memory, thread_temp_file_readed)
thread_oversized_file = #True thread_data_to_read = thread_data_size - thread_data_readed
thread_oversized_file = #True
Else
thread_temp_cache_memory = AllocateMemory(thread_data_size)
ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
CloseFile(thread_file_handle)
thread_oversized_file = #False
EndIf
Else Else
thread_temp_cache_memory = AllocateMemory(thread_data_size) thread_redirect = #True
ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
CloseFile(thread_file_handle)
thread_oversized_file = #False
EndIf EndIf
;} ;}
@ -873,49 +888,41 @@ Module lhs_web
Else Else
;{ Uncached file sems to be stable tested up to 200 clients and 100 requests. ;{ Uncached file sems to be stable tested up to 200 clients and 100 requests.
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead) If file_check(thread_requested) = thread_requested
If Not thread_file_handle thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead)
thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead) If Not thread_file_handle
lhs_log::Out("FileDir:" + configuration\basedir + configuration\defaultfile) thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead)
If Not thread_file_handle lhs_log::Out("FileDir:" + configuration\basedir + configuration\defaultfile)
thread_file_handle = ReadFile(#PB_Any, "error.html") If Not thread_file_handle
EndIf 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
;Do Handle the File another way.
thread_temp_cache_memory = AllocateMemory(configuration\mem\MaxFileSize)
thread_temp_file_readed = configuration\mem\DefaultBlockSize
thread_data_readed = ReadData(thread_file_handle, thread_temp_cache_memory, thread_temp_file_readed)
thread_data_to_read = thread_data_size - thread_data_readed
thread_oversized_file = #True
Else
thread_temp_cache_memory = AllocateMemory(thread_data_size)
ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
CloseFile(thread_file_handle)
thread_oversized_file = 0
EndIf
Else
;Send Redirect:
thread_redirect = #True
EndIf EndIf
thread_data_size = Lof(thread_file_handle)
; Is File bigger than MaxFileSize in Memory allowed ?
If thread_data_size >= configuration\mem\MaxFileSize
;Do Handle the File another way.
thread_temp_cache_memory = AllocateMemory(configuration\mem\MaxFileSize)
thread_temp_file_readed = configuration\mem\DefaultBlockSize
thread_data_readed = ReadData(thread_file_handle, thread_temp_cache_memory, thread_temp_file_readed)
thread_data_to_read = thread_data_size - thread_data_readed
thread_oversized_file = #True
Else
thread_temp_cache_memory = AllocateMemory(thread_data_size)
ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
CloseFile(thread_file_handle)
thread_oversized_file = 0
EndIf
;} ;}
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 Default
@ -926,8 +933,8 @@ Module lhs_web
;* Read Buffer to Memory and Clear Buffer to Zero until the complete Networkbuffer from this Client is Cleaned. ;* Read Buffer to Memory and Clear Buffer to Zero until the complete Networkbuffer from this Client is Cleaned.
;* ;*
EndSelect EndSelect
If thread_data_size If thread_data_size Or thread_redirect = #True
If thread_type ="" If thread_type = ""
thread_type = mimetype(GetExtensionPart(thread_requested)) thread_type = mimetype(GetExtensionPart(thread_requested))
EndIf EndIf
@ -949,14 +956,21 @@ Module lhs_web
If Response(#http_head_set_cookie) <> "" If Response(#http_head_set_cookie) <> ""
Header(#http_head_set_cookie) = Response(#http_head_set_cookie) Header(#http_head_set_cookie) = Response(#http_head_set_cookie)
EndIf 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_content_length) = Str(thread_data_size)
Header(#http_head_content_type) = thread_type
Header(#http_head_connection) = "Keep-Alive" Header(#http_head_connection) = "Keep-Alive"
Header(#http_head_keep_alive) = "timeout=15, max=1000" Header(#http_head_keep_alive) = "timeout=15, max=1000"
thread_header = http_header_generate(Header()) thread_header = http_header_generate(Header())
;large File Handling ;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) thread_buffer = AllocateMemory(thread_data_size+StringByteLength(thread_header)+12)
Else Else
thread_buffer = AllocateMemory(thread_temp_file_readed+StringByteLength(thread_header)+12) thread_buffer = AllocateMemory(thread_temp_file_readed+StringByteLength(thread_header)+12)
@ -967,16 +981,18 @@ Module lhs_web
lhs_log::Out("Header Finished") lhs_log::Out("Header Finished")
EndIf EndIf
;Copy temporary File Buffer to normal Buffer. ;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) CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_data_size)
lhs_log::Out("Cache Address Memory:"+Str(thread_temp_cache_memory)) lhs_log::Out("Cache Address Memory:"+Str(thread_temp_cache_memory))
FreeMemory(thread_temp_cache_memory) FreeMemory(thread_temp_cache_memory)
thread_temp_cache_memory = 0 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) CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_temp_file_readed)
lhs_log::Out("Cache Address Memory:"+Str(thread_temp_cache_memory)) lhs_log::Out("Cache Address Memory:"+Str(thread_temp_cache_memory))
FreeMemory(thread_temp_cache_memory) FreeMemory(thread_temp_cache_memory)
thread_temp_cache_memory = 0 thread_temp_cache_memory = 0
ElseIf thread_redirect = #True
thread_redirect = #False
Else Else
lhs_log::Out("File Buffer Troubles.") lhs_log::Out("File Buffer Troubles.")
If thread_temp_cache_memory = 0 : lhs_log::Out("thread_temp_cache_memory = 0") : EndIf 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()) DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
;Send the data in memory to client. ;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) sent_total = thread_data_size+(thread_buffer_offset-thread_buffer)
Else Else
sent_total = thread_temp_file_readed+(thread_buffer_offset-thread_buffer) sent_total = thread_temp_file_readed+(thread_buffer_offset-thread_buffer)
@ -1271,6 +1289,23 @@ Module lhs_web
EndSelect EndSelect
EndProcedure 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 EndModule

View file

@ -97,6 +97,7 @@ DeclareModule lhs_web
#http_head_keep_alive = "keep-alive:" #http_head_keep_alive = "keep-alive:"
#http_head_cookie = "cookie:" #http_head_cookie = "cookie:"
#http_head_set_cookie = "set-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_application_x_www_form_urlencoded = "application/x-www-form-urlencoded"
#http_content_type_multipart_form_data = "multipart/form-data" #http_content_type_multipart_form_data = "multipart/form-data"