Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

O-script Socket and Web.Write function

Status
Not open for further replies.

jackhals

Programmer
Sep 16, 2008
3
NL
I am receiving content(html, rtf, pdf or video) from an external location via a socket. After I receive this content I am trying to pass it throug to the browser with the Web.Write function. With HTML it works fine but with the other formats it does'nt. How to send these formats to the browser ?
If anyone has any suggestions I would appreciate a lot!!

P.S. this is being done within a Request Handler

 
I think what you are trying to do is to set the content header so the browser recognizes it.Her's an example form the zip/download function.
if IsNotError( f )

// build the headers for the document
//headers = Str.Format( "Content-Type: application/x-zip-compressed%1", Web.CRLF )
headers = Str.Format( "Content-Type: application/octet-stream%1", Web.CRLF )
headers += Str.Format( "Content-Disposition: attachment; filename=%1%2%2", downloadName, Web.CRLF )

// write the headers
Web.Write( ctxout, headers )

// write the file data
for ( b = File.ReadBytes( f, 30720 ); b != File.E_Eof; b = File.ReadBytes( f, 30720 ) )
Socket.Write( ctxout, b )
end

// immediately flush and close the everything (if there is a small amount of data, a second set of text/plain HTML content is appended using the Web.Write functions)
//Socket.Flush( ctxout )
Socket.Close( ctxout )
File.Close( f )
also I write out results from a capi.exec by setting the mime to application/vnd.excel so that the browser opens it up in excel.I hope this was the qn

Well, if I called the wrong number, why did you answer the phone?
James Thurber, New Yorker cartoon caption, June 5, 1937
 
Thank you very much for the effort. What you have described works, except for pdf files.
A bigger problem is that the socket I'm trying to read returns data only if I am in debug mode. (It works fine if I go step by step debugging the code untill I pass the Socket.Flush function)
Do you have any idea?
I am using the following code to make contact to the socket en to read from it:
sDownloadUrl = "application = "GET "+sTemp+" HTTP/1.1"


Socket.Connect( sock, sDownloadUrl, 80 ) // Connection with host
Socket.Write( sock, application ) // Patch to the correct application
Socket.Write( sock, Web.CRLF ) // Line Feed
Socket.Write( sock, "HOST: "+sDownloadUrl ) // needed for protocol http/1.1
Socket.Write( sock, Web.CRLF ) // Line Feed
Socket.Write( sock, Web.CRLF ) // Line Feed
Socket.Flush( sock ) // Flushes the output buffer of the specified Socket.


iIndex = Str.Locate(sTemp, "OC-")
sFileName = sTemp[iIndex:length(sTemp)]

while ( !IsError( status = Socket.ReadBytes( sock ) ) ) //Reading the information from the response
Socket.Write( sock, "" )
strBytes = Bytes.GetString(status, 0)
iIndex = Str.LocateI( strBytes, Web.CRLF+Web.CRLF )

if iIndex && bHeaderDone

sHTTPHeader = strBytes[1:iIndex + 1]
sBody = strBytes[iIndex + 4:length(strBytes)]

lResult = Str.Elements(strBytes, Web.CRLF)
for sTemp in lResult
if Str.LocateI( sTemp, "Content-Length:")
len = Str.StringToInteger( .Strip(sTemp, "Content-Length:", undefined) )
end
if Str.LocateI( sTemp, "Content-Type:")
mimeType = .Strip(sTemp, "Content-Type:", undefined)
end
end


headers = Str.Format( "Content-Type: application/octet-stream%1", Web.CRLF )
headers += Str.Format( "Accept-ranges: bytes%1Content-Type: %2%1Content-Length: %3%1", Web.CRLF, mimeType, len )

if !Str.LocateI( sHTTPHeader, "Content-Disposition")
if ( UseHeaderHint( r ) )
headers += Str.Format( "Content-Disposition: attachment; filename*=utf-8'%1%2%2", sFileName, Web.CRLF )
else
headers += Str.Format( "Content-Disposition: attachment; filename=%1%2%2", sFileName, Web.CRLF )

end

end

sTemp = "C:\temp\test"

File.Create(sTemp)

f = File.Open(sTemp, File.WriteBinMode)

File.WriteBytes(f, Bytes.NewFromString( sBody ))


bHeaderDone = False

elseif Length(status)

File.WriteBytes(f, status)
end

end

Socket.Close( sock )

File.Close( f )

Bytes bTemp

fr = File.Open( sTemp, File.ReadBinMode )

Socket.Write( ctxout, headers )

if ( !IsError( fr ) )
for ( bTemp = File.ReadBytes( fr, 30720 ); \
bTemp != File.E_Eof; \
bTemp = File.ReadBytes( fr, 30720 ) )

Socket.Write( ctxout, bTemp )
end
end
File.Close( fr )

Socket.Close( ctxout )( ctxout )
 
I am not entirely sure what the pdf problem is but in your builder run a search for $Kernek.Mutex.New().That call will wrap the RH having this socket code to be thread safe .When I wrote a webservice to consume a sap webservice I had to wrap my socket code within a thread safe procedure.There are plenty of examples in builder where they use the Mutex.New() call..I also noticed that my builder would freeze when I debug socket lines I am not sure why.Also check the Socket discussion in developer area in discussions area tht is where i got the idea that I could use sockets but it comes with its own price

Well, if I called the wrong number, why did you answer the phone?
James Thurber, New Yorker cartoon caption, June 5, 1937
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top