1212# and conditions defined in the LICENSE file.
1313
1414require "json"
15+ require "uri"
1516require "webrick"
1617
1718module ASRFacet
@@ -122,7 +123,7 @@ def handle_sessions(req, res)
122123 end
123124
124125 def handle_session_lookup ( req , res )
125- session = @session_store . fetch ( req . query [ "id" ] . to_s )
126+ session = @session_store . fetch ( request_param ( req , "id" ) . to_s )
126127 session ? respond_json ( res , { session : session } ) : respond_json ( res , { error : "not_found" } , status : 404 )
127128 rescue StandardError
128129 respond_json ( res , { error : "lookup_failed" } , status : 500 )
@@ -131,7 +132,7 @@ def handle_session_lookup(req, res)
131132 def handle_run ( req , res )
132133 return respond_json ( res , { error : "method_not_allowed" } , status : 405 ) unless req . request_method == "POST"
133134
134- session_id = req . query [ "id" ] . to_s
135+ session_id = request_param ( req , "id" ) . to_s
135136 return respond_json ( res , { error : "not_found" } , status : 404 ) if @session_store . fetch ( session_id ) . nil?
136137
137138 if @session_runner . start ( session_id )
@@ -276,6 +277,24 @@ def parse_json(req)
276277 { }
277278 end
278279
280+ def request_param ( req , key )
281+ key_name = key . to_s
282+ from_query = req . query [ key_name ] . to_s
283+ return from_query unless from_query . empty?
284+
285+ query_string = req . query_string . to_s
286+ if query_string . empty?
287+ request_uri = req . request_uri
288+ query_string = request_uri . query . to_s if request_uri . respond_to? ( :query )
289+ query_string = request_uri . to_s . split ( "?" , 2 ) . last . to_s if query_string . empty? && request_uri . to_s . include? ( "?" )
290+ end
291+ return nil if query_string . empty?
292+
293+ URI . decode_www_form ( query_string ) . to_h [ key_name ]
294+ rescue StandardError
295+ nil
296+ end
297+
279298 def respond_json ( res , payload , status : 200 )
280299 res . status = status
281300 res [ "Content-Type" ] = "application/json; charset=utf-8"
0 commit comments