Announcement

Collapse
No announcement yet.

How to send a JPG to a browser via TCP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to send a JPG to a browser via TCP

    Question:
    BBSDown is a mini webserver, I've parsed the GET command out of the client header and got thinking if a user had an image displayed in a forum, I couldalso send a JPG or GIF etc if I found the request to be FOR a gif/JPG etc.

    So I've parsed the GET, determined whether it's requesting html or an image and I'd like to send the image to the client.

    Now I assumed (bad word) that I could just binarily read the JPG into memory and transmit that out as a string, apparantly not, does not work..

    Wha'ts the trick?

    Here is the FD_READ code where I send the data:

    Code:
    'ParseAndUpdateLogfile reads the client header in and gathers the info from that.
    'IF lResult is FALSE then they requested html, otherwise an image (It's a start)
                    lResult = ParseAndUpdateLogfile(ByVal sPacket,ByVal hTcp) 'Updates window
                    ' <-------------Send data and disconnect------------->
                    Tcp Send hTcp, "HTTP/1.1 200"
                    Tcp Send hTcp, "Server: " & g_szMine
                    Tcp Send hTcp, "Date: " & GetPCTimeandDate()
                    If IsFalse lResult Then 'html
                        Tcp Send hTcp, g_BBSDown
                        Tcp Send hTcp, "Content-Type: text/html"
                        Tcp Send hTcp, "Content-Length: " & Format$(Len(g_BBSDown))
                    Else 'jpg
                        Tcp Send hTcp, "Content-Type: image/jpeg"
                        Tcp Send hTcp, "Content-Length: " & Format$(Len(g_BBSDownImage))
                        Tcp Send hTcp, g_BBSDownImage
                    End If
    
                    Tcp Close hTcp
                    hTcp = %INVALID_SOCKET
                    ' <-------------Send data and disconnect------------->
                    'End connection here
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    Anyone?

    I put a sniffer on last night and grabbed a pic off my website...
    This is what I saw..

    well this is the code so far.
    Code:
                    ' <-------------Send data and disconnect------------->
                    If IsFalse lResult Then 'html
                        Tcp Send hTcp, "HTTP/1.1 200"
                        Tcp Send hTcp, "Server: " & g_szMine
                        Tcp Send hTcp, "Date: " & GetPCTimeandDate()
                        Tcp Send hTcp, "Content-Type: text/html"
                        Tcp Send hTcp, "Content-Length: " & Format$(Len(g_BBSDown))
                        Tcp Send hTcp, g_BBSDown
                    Else 'jpg
                        Tcp Send hTcp, "HTTP/1.1 304"
                        Tcp Send hTcp, "Last-Modified: " & GetFileDateandTime(g_BBSDownImage) & " GMT\r\n"
                        Tcp Send hTcp, "Server: " & g_szMine
                        Tcp Send hTcp, "Date: " & GetPCTimeandDate()
                        Tcp Send hTcp, "Content-Type: image/jpeg"
                        Tcp Send hTcp, "Content-Length: " & Format$(Len(g_BBSDownImage))
                        Tcp Send htcp, "Accept-range: bytes" 'or something like that.
                        Tcp Send hTcp, g_BBSDownImage
                    End If
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      transfer files/pictures

      This is something i am using


      Code:
      select case lcase$(mid$(sUrl,instr(sUrl,".") + 1))
                     case "htm", "html", "bas", "txt", "asp" , "js"
                        Content = "text/html"
                     case "jpg","jpeg"
                        Content = "image/jpeg"
                     case "png"
                        Content = "image/png"   
                     case "gif"
                        Content = "image/gif"
                     case "exe", "php", "com", "bat", "cgi", "cmd", "bin"
                              CgiScript = %TRUE
                        Content = "text/html" ' CGI support 
                     case else
                        Content = "application/octet-stream"
      end select
      
      
      HTML =  GetFile(IPADDRESS, sUrl,STime)
      if len(HTML) = 0 THEN 'File not found Found
         HTML = "<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>Page not found </BODY><HTML>"
      end if
      
      if istrue lIfModified and istrue IsLastModifiedSame(STime,STimeIfModified) then
                        HEADER = "HTTP/1.0 304 Not Modified" + $CRLF
                        HEADER = HEADER +  RespDate + $CRLF
                        HEADER = HEADER + "Server: MGHTTP/2.1" + $CRLF
                        HEADER = HEADER + "Connection: close" + $CRLF                  
                        HTML = ""  
                     else                  
                        HEADER =          "HTTP/1.1 200 OK" + $CRLF
                        HEADER = HEADER +  RespDate + $CRLF               
                        HEADER = HEADER + "Server: MGHTTP/2.1" + $CRLF
                        HEADER = HEADER + "Accept-Ranges: bytes" + $CRLF               
                        HEADER = HEADER + "Connection: close" + $CRLF
                        HEADER = HEADER + "Content-Type: " + Content + $CRLF
                        if GetFileDateAndTime(sUrl,STime) then 'Date Modified
                           HEADER = HEADER + "Cache-Control: private" + $CRLF 
                           HEADER = HEADER + "Last-Modified: " + SYSTEMTIMEToGMT(STime) + $CRLF
                           call DateTimeToSystemTime(dateadd(now,"h", 1),STime)
                           HEADER = HEADER + "Expires: " + SYSTEMTIMEToGMT(STime) + $CRLF
                        end if
                        if lAcceptGzip and _
                           lcase$(mid$(sUrl,instr(sUrl,".") + 1)) = "htm" or _
                           lcase$(mid$(sUrl,instr(sUrl,".") + 1)) = "html" or _
                           lcase$(mid$(sUrl,instr(sUrl,".") + 1)) = "asp" or _
                           lcase$(mid$(sUrl,instr(sUrl,".") + 1)) = "txt" or _
                           lcase$(mid$(sUrl,instr(sUrl,".") + 1)) = "js" or _
                           lcase$(mid$(sUrl,instr(sUrl,".") + 1)) = "cgi" THEN
                           if istrue GzipHTML(HTML,compHTML, 9)  then
                              HEADER = HEADER + "Content-Encoding: gzip" + $CRLF
                              HEADER = HEADER + "Vary: Accept-Encoding" + $CRLF
                              HTML = compHTML
                           end if
                               
                        END IF
                     end if 
                     HEADER = HEADER + "Content-Length: " + LTRIM$(STR$(LEN(HTML))) + $CRLF
                     HEADER = HEADER +  $CRLF
                     sBuffer = HEADER & HTML
                             
                     ssend(ByVal hSocket,ByVal( StrPtr(sBuffer) ), Len(sBuffer), 0)
      
      
      
      
      Steve Miltiadous

      Comment


      • #4
        Got it, I did the header thing, added one $CRLF in and BAM.

        Code:
                        ' <-------------Send data and disconnect------------->
                        If IsFalse lResult Then 'html
                            Header = "HTTP/1.1 200" & $CrLf
                            Header = Header & "Server: " & g_szMine & $CrLf
                            Header = Header & "Date: " & GetPCTimeandDate() & $CrLf
                            Header = Header & "Content-Type: text/html" & $CrLf
                            Header = Header & "Content-Length: " & Format$(Len(g_BBSDown)) & $CrLf
                            Tcp Send hTcp, Header & $CrLf
                            Tcp Send hTcp, g_BBSDown
                        Else 'jpg
                            Header = Header & "HTTP/1.1 304" & $CrLf
                            Header = Header & "Last-Modified: " & GetFileDateandTime(g_BBSDownImageFile) & " GMT" & $CrLf
                            Header = Header & "Server: " & g_szMine & $CrLf
                            Header = Header & "Date: " & GetPCTimeandDate() & $CrLf
                            Header = Header & "Content-Type: image/jpeg" & $CrLf
                            Header = Header & "Content-Length: " & Format$(Len(g_BBSDownImage)) & $CrLf
                            Header = Header & "Accept-range: bytes"  & $CrLf 'or something like that.
                            Header = Header & "Connection: close" & $CrLf
                            Tcp Send hTcp, Header & $CrLf
                            Tcp Send hTcp, g_BBSDownImage
                        End If
                        Tcp Close hTcp
                        hTcp = %INVALID_SOCKET
                        ' <-------------Send data and disconnect------------->
        Last edited by Scott Turchin; 8 Oct 2008, 11:36 PM.
        Scott Turchin
        MCSE, MCP+I
        http://www.tngbbs.com
        ----------------------
        True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

        Comment


        • #5
          Now if my program cannot find the local image, how do I send a 404 not found - jus a header with 404 not found?
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment


          • #6
            Originally posted by Scott Turchin View Post
            Now if my program cannot find the local image, how do I send a 404 not found - jus a header with 404 not found?
            Yes and if the file you want to send is not modified since the "IF-MODIFIED-SINCE:" just send

            HTTP/1.0 304 Not Modified

            you don't need to resend it.
            If the application is threaded put some retries on the file open.




            Steve Miltiadous

            Comment


            • #7
              I'm just assuming say, the user deleted the file, my app does an "If Exists(Byval FileSpec) to check for the file...but if I don't have it I'd like to send a 404....(for the practice of it too
              Scott Turchin
              MCSE, MCP+I
              http://www.tngbbs.com
              ----------------------
              True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

              Comment


              • #8
                The header should return

                "HTTP/1.1 404 Not Found"

                so the browser knows

                but you should also return something for the user like

                Code:
                <html>
                <head>
                <title>Page not found</title>
                </head>
                <body>
                    <b>The page you requested does not exist</b>
                </body>
                
                <html>
                also this code is wrong
                Code:
                Else 'jpg
                    Header = Header & "HTTP/1.1 304" & $CrLf
                    ....
                End If
                change to "HTTP/1.1 200"

                304 tells the browser to use a cached copy and only send if you received this "If_Modified_Since" and your copy has not been modified since that time.



                Steve Miltiadous

                Comment


                • #9
                  Thanks - that fixed the problem of it not showing up for the 3rd or 4th refresh

                  I've released it with that fix it in

                  http://www.tngbbs.com/ccs/bbsdown
                  Scott Turchin
                  MCSE, MCP+I
                  http://www.tngbbs.com
                  ----------------------
                  True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                  Comment

                  Working...
                  X