function parses an absolute path form of the initial line in place.

Team-FLY

19.8 HTTP Header Parsing

In contrast to tunnels, proxies and gateways are party to the HTTP communication and must parse at least the initial line of the client request. This section discusses a parse function that parses the initial request line. The parse function has the following prototype.

 int parse(char *inlin, char **commandp, char **serverp,                  char **pathp, char **protocolp, char **portp); 

The inlin parameter should contain the initial line represented as an array terminated by a line feed. Do not assume in your implementation of parse that inlin is a string, because it may not have a string terminator. The parse function parses inlin in place so that no additional memory needs to be allocated or freed.

The parse function returns 1 if the initial line contains exactly three tokens, or 0 otherwise . On a return of 1, parse sets the last five parameters to strings representing the command, server, path , protocol and port, respectively. These strings should not contain any blanks, tabs, carriage returns or line feeds.

The server and port pointers may be NULL . If an absolute path rather than an absolute URI is given, the server pointer is NULL . If the optional port number is not given, the port pointer is NULL . Allow any number of blanks or tabs at the start of inlin , between tokens, or after the last token. The inlin buffer may have an optional carriage return right before the line feed.

Example 19.23

Figure 19.10 shows the result of calling parse on a line containing an absolute path form of the URI. The line has two blanks after GET and two blanks after the path. The carriage return and line feed directly follow the protocol. The parse function sets the first blank after GET and the first blank after the path to the null character (i.e., '\0' ). The parse function also replaces the carriage return by the null character. The NULL value of the *serverp parameter signifies that no host name was present in the initial inlin , and the NULL value of *portp signifies that no port number was specified.

Figure 19.10. The parse function parses an absolute path form of the initial line in place.

graphics/19fig10.gif

Example 19.24

Figure 19.11 shows the result of parse for a line that contains an absolute URI distinguished by the leading http:// after GET . Notice that parse moves the host name one character to the left so that it can insert a null character between the host name and the path. There is always room to do this, since the leading http:// is no longer needed.

Figure 19.11. The parse function parses the absolute URI form of the initial line by moving the server name to the left.

graphics/19fig11.gif

Implement parse in stages. Start by skipping the leading blanks and tabs, and check that there are exactly three tokens before the first line feed. If inlin does not have exactly three tokens, return 0. Then break these tokens into three strings, setting the command, path and protocol pointers. Consider the second token to be an absolute URI if it starts with http:// and contains at least one additional / character. The server and port pointers should be set to NULL . After successful testing, handle the server pointer. When this is working, check for the port number.

You should write the code to break the input line into strings yourself. Do not use strtok , since it is not thread-safe. Be careful not to assume that the input line is terminated by a string terminator. Do not modify any memory before or after the input line. Test parse by writing a simple driver program. Remember not to assume that the first parameter to parse is a string.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net