Welcome, Guest. Please login or register.

Author Topic: Example of C source code for getting web page.  (Read 8476 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline patrik

Re: Example of C source code for getting web page.
« on: January 21, 2006, 04:34:08 AM »
The request-string should be "GET / HTTP/1.0\r\n\r\n" to get the default page. For another page "GET /somedir/somefile.html HTTP/1.0\r\n\r\n". This will make the HTTP-server respond with a header and then the content.

The easiest way to get the default page is to skip the HTTP-version and make the request-string just "GET /\r\n". This will make the server assume a HTTP/0.9 client and just send the file without any header before it.

When writing a simple client, I would recommend sending "HTTP/1.0" as version, because then you will get the header plus have the possibility to support virtual hosts (which almost all webservers use to share many sites on one the same ip-adress), but not have to support chunked transfer mode.

For example this site requires the client to tell what site he is referring to, as the server is using virtual hosts to host several sites. A request to get the default page of amiga.org would look like this:
Code: [Select]
GET / HTTP/1.0\r\n
Host: amiga.org\r\n\r\n
Without supplying the "Host: amiga.org"-line, the webserver wont know what site you are asking for and will return some default page - try entering the ip-adress for amiga.org in a browser and see what happens then.

\r = Carriage Return (CR = 0x0D)
\n = Line Feed (LF = 0x0A)

This page, when working, has some rather good information.

(edit): Removed errors.


/Patrik
 

Offline patrik

Re: Example of C source code for getting web page.
« Reply #1 on: January 21, 2006, 04:44:34 AM »
@koaftder:

The reason why you are getting a "400 Bad Request" response is because you are specifying that your client is a HTTP/1.1 client, which requires you to supply the "Host: something.com" header-line, which is optional in HTTP/1.0, but required for virtual hosts to work, so it is definately recommended to supply it anyhow.

With a simple client, there is no advantage in telling the server that your client supports HTTP/1.1 instead of HTTP/1.0, rather disadvantages as then the server is allowed to send you dynamic pages as chunks using the so called "chunked transfer-coding".


/Patrik
 

Offline patrik

Re: Example of C source code for getting web page.
« Reply #2 on: January 21, 2006, 05:45:50 AM »
@koaftder:

Dude, no need to send so much! Check my earlier example for amiga.org, which is the only stuff you need to send and should send to make it work with all servers plus make it as easy as possible for you when coding a client. If you dont advertise that your client supports wierd encodings and transfer modes, the server wont utilize such.

This page is rather good regarding what pitfalls there are when designing high performance server software.


/Patrik
 

Offline patrik

Re: Example of C source code for getting web page.
« Reply #3 on: January 21, 2006, 06:35:26 AM »
@AmigaEd:

If targetting for the Amiga, you should take a look at the AmiTCP-SDK which gives you the necessary headers to work with bsdsocket.library (also link-libraries that can do some misc stuff for you, but they are not needed) which is the standard implementation of the BSD sockets API amongst Amiga TCP/IP stacks.

Using bsdsocket.library is not hard, you need to add the AmiTCP-SDKs in your include-path and include , and . Its like using any other shared library - you can utilize its functions after opening it with exec.library/OpenLibrary(). After that, it is more or less identical to programming the BSD sockets API, as far as the networking is concerned.

There are also some examples with the AmiTCP-SDK, even a small HTTP/GET client. If you are new to C, they might not be too straightforward though.


/Patrik
 

Offline patrik

Re: Example of C source code for getting web page.
« Reply #4 on: January 21, 2006, 01:08:14 PM »
@Jose:

Yes, all TCP/IP-stacks on the Amiga, except the obselete AS225, gives you access through bsdsocket.library, even Roadshow - else no existing network applications would work with it.

It is always good to have atleast some knowledge about the protocols you are going to use. Not necessary all the nitty gritty details, but it will give you a much better understanding of how to create applications that are suited for them if you have a fair amount of knowledge about how they work, what their strongpoints and drawbacks are, etc.


/Patrik