Results 1 to 9 of 9

Thread: How do I impliment cgi in my webserver?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Post

    I have written my own web server in vb 5 pro (sp3) and I need to know how to impliment cgi into my webserver. I know how to start the program that is to be used as the cgi. I need to know how to get the stdout variable and how to set the stdin variable. Please give me code or ideas, not .dll or .ocx files. Thank you in advance.

  2. #2
    Junior Member
    Join Date
    May 1999
    Location
    N/A, N/A, England
    Posts
    21

    Post

    for starters you need a perl interpreter running. This is what executes the CGI scripts...

    ------------------
    Second year Software Engineering Student looking for a placement
    http://drinky.u4l.com
    [email protected]


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Post

    Not everybody uses perl for webservers. In every other server I've seen, it could load any program (including .exe files) and, if the program was set up to do it, it could grab the output from the .exe file. How, in vb, do i access stdin, stdout, or stderr?

  4. #4
    Guest

    Post

    agent,
    When you say you wrote your own web server, what exactly did you code? For CGI, you need to capture this info:VARIABLE_NAME Value
    DOCUMENT_ROOT The root directory of your server
    HTTP_COOKIE The visitor's cookie, if one is set
    HTTP_HOST The hostname of your server
    HTTP_REFERER The URL of the page that called your script
    HTTP_USER_AGENT The browser type of the visitor
    PATH The system path of your server (usally /bin, /usr/sbin, etc)
    QUERY_STRING The query string
    REMOTE_ADDR The IP address of the visitor
    REMOTE_HOST The hostname of the visitor (if your server has reverse- name-lookups on; otherwise this is the IP address again)
    REMOTE_PORT The port the visitor is connected to on the web server
    REMOTE_USER The visitor's username (for .htaccess-protected pages)
    REQUEST_METHOD GET or POST
    SCRIPT_FILENAME The full pathname of the current CGI
    SERVER_ADMIN The email address for your server's webmaster
    SERVER_NAME Your server's fully qualified domain name (e.g. www.domainname.com )
    SERVER_PORT The port number your server is listening on
    SERVER_SOFTWARE The server software you're using (such as "agent 1.3" ???)

    I consider the above as variables and stdin, stdout, stderr as STREAMS.
    And yes, you do not have to limit yourself to using perl. I don't think you want to do this in VB, but wouldn't reading from and writing to a socket be the closest thing to stdin and stdout?

    Ciao,
    VirtuallyVB (around here)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Post

    to answer you question as to what exactly i coded: I sucessfully coded an HTTP file server (a web server the a browser (eg netscape) can download file from) I have seen cgi programs in vb access those streams before. It uses some api that is beyond my level. (my server uses little api) It looks like file operations api to me. If you want the bas file that shows you how to write cgi in vb, I can get it to you if you ask.

    b.t.w. stdout is used by the cgi to comunicate to the server. the server relays the contents of stdout tho th www.
    stdin sends the queries (i.e. from forms) to the server, then to the cgi.
    I am not absolutly sure what stderr is for.

    [This message has been edited by agent (edited 06-19-1999).]

  6. #6
    Hyperactive Member
    Join Date
    Jul 1999
    Location
    NY, USA
    Posts
    270

    Post

    CGI Scripts usually start with a

    #!/usr/local/bin/perl

    This says to execute /usr/local/bin/perl with the standard input of the rest of the file... I know a lot about PERL and web servers, so let me know if you need anymore help.

    ------------------
    Tom Young, 14 Year Old
    [email protected]
    ICQ: 15743470
    AIM: TomY10
    PERL, JavaScript and VB Programmer

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Post

    Thanks for your help, but what I am trying to do is get access to the streams StdIn and StdOut so I can pass StdIn to the Perl interpreter and pass StdOut (from the perl interpreter) to the browser. When I use those streams, the CGI does not always HAVE to be in Perl. It could be C(++) or even a dos Batch file.

    BTW, that line in some scripts is used to tell the server which interpreter to use, and where it's at. If the server already knows, then this line is ignored. Most Windows Servers i've used also support compiled CGI (actual compiled windows (or dos) programs) and they don't need the slash-bang because they are executed on the spot. Normally they need to be proceded by a .com or .exe though)

  8. #8
    New Member
    Join Date
    Oct 1999
    Location
    St. Louis, MO, USA
    Posts
    8

    Post

    I've done what your asking for and if I get a chance, I'll look up the APIs for you tomorrow. You are correct in saying that CGI does not have to be written in PERL, CGI is 'open' like most things on the Internet, thus it can be written in any language. The only part I'm not sure about is how you're going to enable CGI. I've been out of the CGI coding bit for a while as I've been using ASPs for the last couple of years (yes, I know ASPs use CGI to communicate, but all the CGI stuff is hidden from me by the ISAPI DLL that makes the ASPs work). The part that I'm not sure about is what you would code to make CGI work, like I said, I've coded CGI scripts in VB, but I was already using IIS, which I'm guessing was doing something to enable CGI. Even with IIS I still had to get the standard in and standard out file handles. Maybe the WC3 publish something that describes what you webserver needs to do to enable CGI, anyway, like I said, I'll try to post the API stuff here tomorrow.

    --------------
    WebMan

    [This message has been edited by webman (edited 10-19-1999).]

  9. #9
    New Member
    Join Date
    Oct 1999
    Location
    St. Louis, MO, USA
    Posts
    8

    Post

    Ok, here's the code. I believe I was using the 32-bit version of VB4 when I did this, so hopefully it still works.

    'Declare the needed API functions
    Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
    Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long

    'Constants that will be used in the API functions
    Public Const STD_INPUT_HANDLE = -10&
    Public Const STD_OUTPUT_HANDLE = -11&

    'Remember we are using 32bit and 32bit calls to the API so we need to use Long instead of Integer
    Global llStdIn As Long
    Global llStdOut As Long
    Global lsBuff As String
    Global llBytesRead As Long
    Global llResult As Long
    Global postData As String
    Global ThisFar As String

    Global CountIt As Long

    '============================
    ' Get the CGI data from STDIN
    '============================
    ' Data is collected as a single string. We will read it 100 bytes at a time.
    '
    Sub GetCGIpostData()
    Dim junk

    'read the standard input handle

    llStdIn = GetStdHandle(STD_INPUT_HANDLE)
    postData = ""
    llBytesRead = 100

    'get POSTed CGI data from STDIN
    Do Until llBytesRead < 100
    lsBuff = String(100, 0) ' Create a buffer big enough to hold the 1024 bytes
    llBytesRead = 100 ' Tell it we want at least 1024 bytes
    junk = ReadFile(llStdIn, ByVal lsBuff, 100, llBytesRead, ByVal 0&) 'Read the data
    If llBytesRead > 0 Then ' Check the amout of data read in to see if we are figure
    postData = postData & Left(lsBuff, llBytesRead) ' Add the data to our string
    End If
    Loop

    End Sub

    '======================
    ' Send output to STDOUT
    '======================
    '
    Sub Send(s As String)

    Dim llResult As Long
    s = s & vbCrLf
    WriteFile GetStdHandle(STD_OUTPUT_HANDLE), s, Len(s), llResult, ByVal 0&

    End Sub

    ------------------
    WebMan


    [This message has been edited by webman (edited 10-20-1999).]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width