@@ -71,6 +71,11 @@ def handle_request(socket)
7171 socket . write "HTTP/1.1 100 Continue\r \n \r \n "
7272 end
7373
74+ # Set default Content-Type if not provided
75+ if !headers [ 'Content-Type' ] && ( method == 'POST' || method == 'PUT' || method == 'PATCH' )
76+ headers [ 'Content-Type' ] = 'application/octet-stream'
77+ end
78+
7479 req = Request . new ( method , path , headers , socket )
7580 if @procs . key? ( req . path ) || @procs . key? ( "#{ req . path } /" )
7681 proc = @procs [ req . path ] || @procs [ "#{ req . path } /" ]
@@ -306,16 +311,18 @@ def handle_post(path, headers, socket)
306311 scheme = headers [ 'X-Request-Scheme' ] || 'http'
307312 host = @config [ 'host' ]
308313 port = socket . addr [ 1 ]
309- charset = parse_content_type ( headers [ 'Content-Type' ] ) [ 1 ]
314+ content_type = headers [ 'Content-Type' ] || 'application/octet-stream'
315+ charset = parse_content_type ( content_type ) [ 1 ]
310316 path = "#{ scheme } ://#{ host } :#{ port } #{ path } "
311317 path = path . encode ( charset ) if charset
312- response = "HTTP/1.1 200 OK\r \n Content-Type: #{ headers [ 'Content-Type' ] } \r \n Content-Length: #{ body . bytesize } \r \n X-request-uri: #{ path } \r \n \r \n #{ body } "
318+ response = "HTTP/1.1 200 OK\r \n Content-Type: #{ content_type } \r \n Content-Length: #{ body . bytesize } \r \n X-request-uri: #{ path } \r \n \r \n #{ body } "
313319 socket . print ( response )
314320 end
315321
316322 def handle_patch ( path , headers , socket )
317323 body = socket . read ( headers [ 'Content-Length' ] . to_i )
318- response = "HTTP/1.1 200 OK\r \n Content-Type: #{ headers [ 'Content-Type' ] } \r \n Content-Length: #{ body . bytesize } \r \n \r \n #{ body } "
324+ content_type = headers [ 'Content-Type' ] || 'application/octet-stream'
325+ response = "HTTP/1.1 200 OK\r \n Content-Type: #{ content_type } \r \n Content-Length: #{ body . bytesize } \r \n \r \n #{ body } "
319326 socket . print ( response )
320327 end
321328
0 commit comments