Tuesday, August 26, 2014

Serving http requests with tcpserver



I'm trying to use tcpserver from ucspi-tcp to launch a script that returns a simple web page. My script (hello.lua) is as follows:




#!/usr/bin/env lua
print([[HTTP/1.1 200 OK
Content-Type: text/html


My title


Hello in big font



]])



I launch it with tcpserver -v -rh 0 9000 /path/to/hello.lua



When I use tcpcat myserver 9000, I get the expected return:
HTTP/1.1 200 OK
Content-Type: text/html


My title


Hello in big font





However, when I try to use a webbrowser and point to http:\\myserver:9000, I get a browser error (in Chrome) Error 101 (net::ERR_CONNECTION_RESET): Unknown error, even though the tcpserver logging shows a transaction:
tcpserver: status 1/40
tcpserver: pid 21672 from
tcpserver: ok 21672 ::::ffff::9000 ::::ffff:::3133
tcpserver: end 21672 status 0



I know I'm missing something basic here, but I can't put the pieces of the puzzle together. Any insight is appreciated! Thanks!


Answer




You need a blank line between the headers and the content:



HTTP/1.1 200 OK
Content-Type: text/html



My title



Hello in big font





No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...