*	open an HTTP connection (text only) for CSNOBOL4
*	Phil Budne <phil@ultimate.com>
*	Sept 24, 1997
*	$Id: http.sno,v 1.8 2010/12/22 08:19:16 phil Exp $

-include "tcp.sno"

*	whatever needs to be appended to line to make a CRLF
	HTTP.EOL = CHAR(13)

	HTTP.CRLF = CHAR(13) CHAR(10)

*	record length (and data mode) for HTTP.OPEN/POST
	HTTP.RECL = 32767

****************
*	HTTP.OPEN()
*
*	args;
*	Name to associate for I/O
*	Internet hostname
*	Filename (needs to start with "/"?)
*	TCP port (or null)

	DEFINE("HTTP.OPEN(HTTP.NAME,HTTP.HOST,HTTP.FILE,HTTP.PORT)")
+							:(HTTP.OPEN.END)

HTTP.OPEN
	HTTP.PORT = IDENT(HTTP.PORT) "80" 
	TCP.OPEN(HTTP.NAME,HTTP.HOST,HTTP.PORT,HTTP.RECL)	:F(FRETURN)
	$HTTP.NAME = "GET " HTTP.FILE " HTTP/1.0" HTTP.CRLF
+		"User-Agent: " HOST() HTTP.CRLF
+		"Host: " HTTP.HOST HTTP.CRLF
+		HTTP.EOL				:(RETURN)
HTTP.OPEN.END


****************
*	HHTP.CLOSE
*
*	args;
*	I/O var

	DEFINE("HTTP.CLOSE(HTTP.NAME)")			:(HTTP.CLOSE.END)
HTTP.CLOSE
	TCP.CLOSE(HTTP.NAME)				:(RETURN)
HTTP.CLOSE.END

****************
*	HTTP.POST()
*	August 19, 2004
*
*	args;
*	Name to associate for I/O (for reading results)
*	Internet hostname
*	Filename (needs to start with "/"?)
*	TCP port (or null)
*	Data to post
 
	DEFINE("HTTP.POST(HTTP.NAME,HTTP.HOST,HTTP.FILE,HTTP.PORT,HTTP.DATA)")
+							:(HTTP.POST.END)

HTTP.POST
	HTTP.PORT = IDENT(HTTP.PORT) "80" 
	TCP.OPEN(HTTP.NAME,HTTP.HOST,HTTP.PORT,HTTP.RECL) :F(FRETURN)
	$HTTP.NAME = "POST " HTTP.FILE " HTTP/1.0" HTTP.EOL
	$HTTP.NAME = "Host: " HTTP.HOST HTTP.EOL
	$HTTP.NAME = "Content-Length: " SIZE(HTTP.DATA) HTTP.EOL
	$HTTP.NAME = "User-Agent: " HOST() HTTP.EOL
	$HTTP.NAME = HTTP.EOL
	$HTTP.NAME = HTTP.DATA				:(RETURN)
HTTP.POST.END
