/* REXX ************************************************/ /* */ /* Return header for specified URL */ /* */ /* Parameters: */ /* Input URL in the form http://server/document */ /* (case sensitive) */ /* */ /********************************************************/ /* Initialize socket set */ x = SocketCall('Initialize', 'Urlcheck') If SockRc \= 0 Then Do Say 'Unable to initialize environment, RC='SockRc Say 'Returned information=' x Exit 12 End SIGNAL ON HALT NAME TermSock /* Isolate server name and document name */ /* document name is preceeded with a slash */ Parse Arg "http://" Server "/" Document Document = "/" || Document Socket = Connect(Server) If Socket = -1 Then Nop /* unable to connect */ Else Do /* request the header for the specified URL */ Command = "HEAD" Document "HTTP/1.0" Header = SendCommand(Socket, Command) Call Close Socket /* break out header contents */ Do Until len = 0 len = Index(Header, "25"x) /* LF terminator */ If len > 1 then , Say Strip(Substr(Header, 1, len-1), , "0D"x) /* remove CR */ Header = Substr(Header, len+1) End End TermSock: x = SocketCall('Terminate', 'Urlcheck') Exit 0 /********************************************************/ /* */ /* Function: Connect */ /* Purpose: Create a socket and connect it to server. */ /* Arguments: Server - server name,optional:port number */ /* Returns: Socket number if successful, -1 otherwise */ /* */ /********************************************************/ Connect: Procedure Parse Arg Server /* if the servername has a port address specified */ /* then use this one, otherwise use the default http */ /* port 80 */ Parse Var Server Server ":" Port If Port = "" Then Port = 80 /* resolve server name alias to dotted IP address */ Host = SocketCall('Gethostbyname', Server) If SockRc \= 0 Then Do Say "Unable to resolve server:" Server Say Host Return -1 End /* create a TCP socket */ SockId = SocketCall('Socket', '2', 'SOCK_STREAM', '0') If SockRc \= 0 Then Do Say "Unable to create socket" Return -1 End /* connect the new socket to the specified server */ x = Socket = SocketCall('Connect', SockId, 'AF_INET 'port' 'Host) If SockRc < 0 Then Do Say "Unable to connect to server:" Server Call Close SockId Return -1 End /* we need to do this because we are EBCDIC */ x = SocketCall('SetSockOpt', SockId, 'Sol_Socket', 'So_ASCII', 'On') If SockRc < 0 Then Do Say "Unable to set ASCII option" Call Close SockId Return -1 End Return SockId /********************************************************/ /* */ /* Function: SendCommand */ /* Purpose: Send a command via the specified socket */ /* and return the full response to caller. */ /* Arguments: Socket - active socket number */ /* Command - command string */ /* Returns: Response from server or empty string if */ /* failed. */ /* */ /********************************************************/ SendCommand: Procedure Parse Arg Socket, Command /* append two pairs of CRLF to end the command string */ Command = Command || "0D250D25"x /* note: 25 is an ebcdic LF */ BytesSent = SocketCall('Send', Socket, Command) Response = "" /* assemble the received data into one string */ Do Forever RecvData = SocketCall('Recv', Socket, 1024) Parse Var RecvData BytesRcvd RcvData If BytesRcvd <= 0 Then Leave Response = Response || RcvData End Return Response /********************************************************/ /* */ /* Procedure: Close */ /* Purpose: Close the specified socket. */ /* Arguments: Socket - active socket number */ /* Returns: nothing */ /* */ /********************************************************/ Close: Procedure Parse Arg Sockid x = SocketCall('Shutdown', Sockid, '2') x = SocketCall('Close', Sockid) Return /********************************************************/ /* */ /* Procedure: SocketCall */ /* Purpose: Invoke a REXX Socket function */ /* Arguments: variable */ /* Returns: socket info */ /* set variable SockRc */ /* */ /********************************************************/ SocketCall: Procedure Expose SockRc Parse Arg service, parm1, parm2, parm3, parm4, parm5 Retinfo = Socket(service, parm1, parm2, parm3, parm4, parm5) Parse Var Retinfo SockRc SockRet Return SockRet