CBGet: HTTP in Chipmunk Basic

Now at https://github.com/anoved/Chipmunk-Basic-Stuff/tree/master/CBGet%20HTTP


cbget stands for “Chipmunk Basic Getter,” because it’s a cbas program that gets web pages. This program was written as an exercise to figure out what sockets were and how to work with them in Chipmunk Basic. It also provided an opportunity to learn more about HTTP.

Usage: ./cbget.bas [host port path [file]]

Called with no arguments or from within the Macintosh GUI version of Chipmunk Basic, cbget prompts the user for input interactively. As indicated above, it is possible to provide the same configuration via command line arguments. As you can see from the following example, the
interactive input fields correspond directly to the command line arguments (pressing Return without entering a file name is the same as omitting that argument):

>run
Host? anoved.net
Port? 80
Path? /cbget.html
File?
...

If no file is given, the contents of the requested resource are printed to the console. If a file is given, the content is saved to that file instead (note the clever use of file #0).

Host/Port/Path

If you want to retrieve a web page with a URL such as http://www.w3.org/Protocols/HTTP/1.1/spec.html, the host you provide to cbget is www.w3.org and the path is the remainder of the URL: /Protocols/HTTP/1.1/spec.html. Port 80 is typically the default port for HTTP connections.

Download

Download cbget 1.0.1 [4k .bas]

You may need to chmod +x cbget.bas if you wish to run it directly from the command line.

It should be quite straightforward to adapt this code to other simple network tasks.

Wish List

  1. accept regular URLs as input
  2. better newline handling
  3. print response headers (easy)
  4. follow HTTP redirects

Version History

1.0.1
Added Connection: close HTTP request header to prevent
post-transfer hang due to unsolicited “keep-alive” connections