Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a3fc0173cae4c43d9947d71ccdb7ca4627d9ec41
Ticket: 6ca52aec14e0b33543d3cd9895f060b852ac4dbc
HTTP package: Memory leak if client requests "Connection: close" but server responses with "Connection: keep-alive"
User & Date: anonymous on 2015-07-17 05:23:22
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    Hello,
    The 'geturl' command of the 'http' package does not free up properly the used variables if geturl if a GET request is made and the '-keepalive' option is not used ('Connection: close' is transmitted), but if the server responses (by mistake) with 'Connection: keep-alive'.
    
    After running 10 times ...
    
       set Handle [::http::geturl "http://localhost:8080/c"]
       puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
       ::http::cleanup $Handle
    
    ... and having the server responding with 'Connection: keep-alive' the following variables will be defined in the http namespace (info vars http::*):
    
       ::http::http ::http::urlTypes ::http::1 ::http::encodings ::http::2 ::http::3 ::http::4 ::http::5 ::http::6 ::http::7 ::http::8 ::http::9 ::http::strict ::http::formMap ::http::defaultKeepalive ::http::socketmap ::http::defaultCharset
    
    The variables 1..9 are the temporary variables that should be deleted with the 'http::cleanup' command.
    
    Below there is a test case composed by 2 parts (files):
    
    * HTTP server: It opens and listens the local port 8080. It accepts 3 requests (http://localhost:8080/a, http://localhost:8080/b, http://localhost:8080/c) that are responded in 3 different ways: Request 'a' will not return any 'Connection' attribute, request 'b' will return 'Connection: close', and request 'c' will return 'Connection: keep-alive'.
    * HTTP client: It performs for each of the 3 requests accepted by the HTTP server 10 requests. After each request the variables in the 'http' namespace are printed.
    
    ------------------------- File 'Run MemoryLeak_TestCase_Server.tcl' -------------------------
    
    namespace eval HttpServer {
    	proc Start {port} {
    		puts "HttpServer::Start $port"
    		set Server [socket -server [namespace current]::Accept $port]
    	}
    
    	proc Accept {sock host port} {
    		puts "HttpServer::Accept $sock $host $port"
    		fconfigure $sock -blocking 0 -buffering none
    		fileevent $sock readable [list [namespace current]::Handle $sock]
    	}
    
    	proc Handle {Socket} {
    		puts "HttpServer::Handle $Socket"
    		if {[eof $Socket]} {
    			puts "  eof->close socket"
    			close $Socket
    			return
    		}
    
    		puts "  Read data"
    		set Data [read $Socket]
    		regexp -line {^(.*)$} $Data {} FirstLine
    		regsub -all -line {^} $Data {   -> } Data
    		puts $Data
    
    		if {[regexp {GET /(.*) HTTP/1.1} $FirstLine {} GetArgs]} {
    			puts "  GetArgs:$GetArgs"
    			puts "  Return data"
    			if {$GetArgs=="a"} {
    				puts $Socket "HTTP/1.1 200 OK"
    				puts $Socket ""
    				puts $Socket "Data received on [clock seconds] - a"
    			} elseif {$GetArgs=="b"} {
    				puts $Socket "HTTP/1.1 200 OK"
    				puts $Socket "Connection: close"
    				puts $Socket ""
    				puts $Socket "Data received on [clock seconds] - b"
    			} elseif {$GetArgs=="c"} {
    				puts $Socket "HTTP/1.1 200 OK"
    				puts $Socket "Connection: keep-alive"
    				puts $Socket ""
    				puts $Socket "Data received on [clock seconds] - c"
    			} else {
    				puts $Socket "HTTP/1.1 404 not found"
    				puts $Socket ""
    				puts $Socket "<html><h1>404 - Not found '$GetArgs'</h1></html>"
    			}
    		} else {
    			puts $Socket "HTTP/1.1 400 bad request"
    			puts $Socket ""
    			puts $Socket "<html><h1>400 - Bad request</h1></html>"
    		}
    
    		puts "  Close socket $Socket"
    		close $Socket
    	}
    }; # end namespace HttpServer
    
    HttpServer::Start 8080
    
    ------------------------- File 'Run MemoryLeak_TestCase_Client.tcl' -------------------------
    
    package require http
    # source ./http-2.8.5.tm
    
    proc ListVarHttp {} {
    	puts "HttpVars([llength [info vars http::*]]): [info vars http::*]"
    }
    
    ListVarHttp
    
    puts "GetUrl http://localhost:8080/a"
    for {set k 0} {$k<10} {incr k} {
    	set Handle [::http::geturl "http://localhost:8080/a"]
    	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
    	::http::cleanup $Handle
    
    	ListVarHttp
    }
    
    puts "GetUrl http://localhost:8080/b"
    for {set k 0} {$k<10} {incr k} {
    	set Handle [::http::geturl "http://localhost:8080/b"]
    	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
    	::http::cleanup $Handle
    
    	ListVarHttp
    }
    
    puts "GetUrl http://localhost:8080/c"
    for {set k 0} {$k<10} {incr k} {
    	set Handle [::http::geturl "http://localhost:8080/c"]
    	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
    	::http::cleanup $Handle
    
    	ListVarHttp
    }
    
  5. foundin changed to: "HTTP package 2.7.10 and 2.8.5"
  6. is_private changed to: "0"
  7. login: "anonymous"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "c7f3b5a0831cb53da82e8d0adc0c229e226e82cc"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "anonymous"
  14. subsystem changed to: "29. http Package"
  15. title changed to:
    HTTP package: Memory leak if client requests "Connection: close" but server responses with "Connection: keep-alive"
    
  16. type changed to: "Bug"