@@ -81,7 +81,7 @@ var GuestSN = flag.String("guestsn", "0000000000000", "Guest serial number")
8181var GuestCPU_Arch = flag .String ("cpu_arch" , "QEMU, Virtual CPU, X86_64" , "CPU arch" )
8282
8383var ApiPort = flag .String ("api" , ":2210" , "API port" )
84- var ApiTimeout = flag .Int ("timeout" , 45 , "API timeout" )
84+ var ApiTimeout = flag .Int ("timeout" , 10 , "Default timeout" )
8585var ListenAddr = flag .String ("addr" , "0.0.0.0:12345" , "Listen address" )
8686
8787func main () {
@@ -308,13 +308,31 @@ func read(w http.ResponseWriter, r *http.Request) {
308308 defer Writer .Unlock ()
309309
310310 query := r .URL .Query ()
311- commandID , err := strconv .Atoi (query .Get ("command" ))
311+ cmd := query .Get ("command" )
312+ timeout := query .Get ("timeout" )
313+ wait := time .Duration (* ApiTimeout )
314+
315+ if len (strings .TrimSpace (cmd )) == 0 {
316+ fail (w , "No command specified" )
317+ return
318+ }
319+
320+ commandID , err := strconv .Atoi (cmd )
312321
313322 if err != nil || commandID < 1 {
314- fail (w , fmt .Sprintf ("Failed to parse command %s" , query . Get ( "command" ) ))
323+ fail (w , fmt .Sprintf ("Failed to parse command: %s" , cmd ))
315324 return
316325 }
317326
327+ if len (strings .TrimSpace (timeout )) > 0 {
328+ duration , err := strconv .Atoi (timeout )
329+ if err != nil || duration < 1 {
330+ fail (w , fmt .Sprintf ("Failed to parse timeout: %s" , timeout ))
331+ return
332+ }
333+ wait = time .Duration (duration )
334+ }
335+
318336 if Connection == nil || Chan == nil {
319337 fail (w , "No connection to guest" )
320338 return
@@ -339,7 +357,7 @@ func read(w http.ResponseWriter, r *http.Request) {
339357 select {
340358 case res := <- Chan :
341359 resp = res
342- case <- time .After (time . Duration ( * ApiTimeout ) * time .Second ):
360+ case <- time .After (wait * time .Second ):
343361 atomic .StoreInt32 (& WaitingFor , 0 )
344362 fail (w , fmt .Sprintf ("Timeout while reading command %d from guest" , commandID ))
345363 return
@@ -374,10 +392,17 @@ func write(w http.ResponseWriter, r *http.Request) {
374392 }
375393
376394 query := r .URL .Query ()
377- commandID , err := strconv .Atoi (query .Get ("command" ))
395+ cmd := query .Get ("command" )
396+
397+ if len (strings .TrimSpace (cmd )) == 0 {
398+ fail (w , "No command specified" )
399+ return
400+ }
401+
402+ commandID , err := strconv .Atoi (cmd )
378403
379404 if err != nil || commandID < 1 {
380- fail (w , fmt .Sprintf ("Failed to parse command %s" , query . Get ( "command" ) ))
405+ fail (w , fmt .Sprintf ("Failed to parse command: %s" , cmd ))
381406 return
382407 }
383408
0 commit comments