/* Simple web server */ /* Default port is 80 */ /* will simply echo all requests*/ /* The following can turn on trace, e.g. trace 'i' */ trace 'o' If RxFuncQuery("SockDropFuncs") then do rc = RxFuncAdd("SockLoadFuncs","rxsock","SockLoadFuncs") call SockLoadFuncs end listen_address.family = 'AF_INET' listen_address.port = 80 listen_address.addr = '0.0.0.0' /* Open a socket */ listen_socket = SockSocket('AF_INET', 'SOCK_STREAM', 'IPPROTO_TCP') /* Set socket options so we can repeat program */ call SockSetSockOpt listen_socket, 'SOL_SOCKET', 'SO_REUSEADDR', 1 /* Bind to the port */ Call SockBind listen_socket, listen_address. /* Listen for a connection */ Call SockListen listen_socket, 1 do forever /* Accept a connection */ conn_socket = SockAccept(listen_socket) /* Receive some data */ data = '' Call SockRecv conn_socket, 'DATA', 1024 say 'Request' data /* Send a response */ call SockSend conn_socket, getdata(data) /* Close connection */ call SockClose conn_socket end exit 0 /* Takes the data from the client and returns the data */ getdata: Procedure parse arg data crlf = '0d0a'x resp = 'HTTP/1.1 200 OK' || crlf say 'Echo request' resp = resp || 'Content-Length:' || length(data) || crlf resp = resp || 'Content-Type: text/html; charset=iso-8859-1' || crlf resp = resp || crlf resp = resp || data return resp