*	open an HTTP connection (text only) for CSNOBOL4
*	Phil Budne <phil@ultimate.com>
*	Sept 24, 1997
*	$Id: http.sno,v 1.10 2020-11-27 02:47:44 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)

****************
*	HTTP.OPEN()
*
*	args;
*	Name to associate for I/O
*	Internet hostname
*	Filename (needs to start with "/"?)
*	TCP port (or null)
*	I/O mode (pass 'B,<recl>' for binary file)
	DEFINE("HTTP.OPEN(HTTP.NAME,HTTP.HOST,HTTP.FILE,HTTP.PORT,HTTP.MODE)")
	DEFINE("HTTPS.OPEN(HTTP.NAME,HTTP.HOST,HTTP.FILE,HTTP.PORT,HTTP.MODE)")
+							:(HTTP.OPEN.END)

HTTPS.OPEN
	HTTP.PORT = IDENT(HTTP.PORT) "443" 
	TCP.OPEN(HTTP.NAME,HTTP.HOST,HTTP.PORT,HTTP.MODE,1)
+							:F(FRETURN)S(HTTP.GET)

HTTP.OPEN
	HTTP.PORT = IDENT(HTTP.PORT) "80" 
	TCP.OPEN(HTTP.NAME,HTTP.HOST,HTTP.PORT,HTTP.MODE)	:F(FRETURN)
HTTP.GET
+	$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
