Rajagiri School of Engineering & Technology: Rajagiri Valley, Kakkanad, Cochin - 682 039
Rajagiri School of Engineering & Technology: Rajagiri Valley, Kakkanad, Cochin - 682 039
Index
HTTP3 SMTP7 POP311 CGI13 SAMPLE PROGRAM.16
The header from the server contains information about the server software and the document sent to the client. Date: Wed, 16 Aug 2000, 13:25:54 GMT Server: NCSA/1.5.2 Last-modified: Sat, 22 Jan 2000, 05:15:43 Content-type: text/html Content-length: 12443 The header is followed by a blank line that indicates the end of the header information. From the example above, the server sends an html document of size 12443 bytes as shown by the Content-type and Contentlength lines. The server line gives details about the server software. The HTTP is a stateless protocol, which means that the connection between the browser and the server is lost once the transaction ends.
1.1.1
It is used to retrieve HTML documents from the server. It is a part of the request line from the client and tells the server to pass a copy of the document back to the browser or run a CGI program. GET is also used to send user information from an HTML form. Through this, the data is sent as a part of the URL in 'name-value' pairs. Suppose your HTML form looks like this: <FORM NAME="myform" ACTION="cgi-bin/validate.cgi" METHOD="GET"> First Name: <INPUT TYPE="TEXT" NAME="fname" SIZE="20"><BR> Last Name: <INPUT TYPE="TEXT" NAME="lname" SIZE="20"> <BR> <INPUT TYPE="SUBMIT" VALUE="SUBMIT"> </FORM> This code will be displayed in the browser as:
When you submit this form, the data is attached to the URL in name-value pairs. Each name-value pair demarcated by a '&'. Data is separated from the actual URL with a '?' sign. Thus after submission the address/location bar of the browser will show us as: www.sitename/cgi-bin/validate.cgi?fname=f1&lname=l1 You can also call CGI programs by supplying data with the URL in a HTML link like: <A HREF="http://www.somesite.com/cgi-program.cgi?name1 =value1&name2=value2>Link to somesite</A> which shows up as: Link to somesite If you move your mouse over this link, you will be able to see the URL and the attached data in the status bar of your browser.
1.1.2
POST
Data from the POST method is sent by the client as a part of the request body. The server receives this data and sends it to a CGI program along with other variables. The METHOD attribute inside the <FORM> tag will carry POST as its value. The big advantage in using POST over GET is that the data is not open to prying eyes. Also, via GET, you can transfer only a limited amount of data; POST exerts no such limitations. The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result. 1.1.3 HEAD The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request should be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification. 1.1.4 PUT The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity should be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource,
and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server must inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes should be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response should be given that reflects the nature of the problem.
1.1.5 DELETE The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method may be overridden by human intervention (or other means) on the origin server. The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully. However, the server should not indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location. A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.
2. SMTP
SMTP or Simple Mail Transfer Protocol is a way to transfer email reliably and efficiently. SMTP is a relatively simple, text-based protocol, where one or more recipients of a message are specified (and in most cases verified to exist) and then the message text is transferred. SMTP is the language that mail servers use to communicate among themselves. Since this protocol started out as purely ASCII text-based, it did not deal well with binary files. Standards such as MIME were developed to encode binary files for transfer through SMTP. SMTP is used to send mail to the recipient's mailbox. The recipient may use various methods to access the emails in his mailbox. A couple of methods that are very popular are POP3 and IMAP. These protocols allow a user to access his messages stored on a remote mail server.
<SP> stands for a space and <CRLF> stands for a combination of Carriage Return and Linefeed. The receiver-SMTP identifies itself to the sender-SMTP in the connection greeting reply, and in the response to this command.
This command does not affect any parameters or previously entered commands. It specifies no action other than that the receiver send an OK reply. NOOP <CRLF>
10
3. POP3
It is not possible for all computers to be continuously connected to the internet. These computers cannot run their own SMTP servers to manage incoming and outgoing mails because the server needs to be running and connected to the internet all the time. Imagine a situation where someone can only send mail to you while you are logged onto the Internet. This situation is certainly unacceptable and ways have been developed to remedy it. One solution is to maintain a mailbox on a machine (the server) which is continuously connected to the Internet. This machine would accept all mails on our behalf. We (the clients) could periodically connect to this server via the Internet to view or download the mails in our mailbox using a agreed upon protocol. Some of the ways to access ones mailbox are listed below: POP3 or the Post Office Protocol - Version 3 is one simple protocol that allows users to access their mailbox on a remote machine. POP3 does not provide "extensive manipulation operations of mail on the server". It was designed so that clients could download the mails from the servers which would then delete them. IMAP4 or the Internet Message Access Protocol - Version 4 was designed as a more powerful way of accessing and manipulating mail messages on a server. It was designed with the idea that a user would like to keep all his mails on the server, which would then be accessible from any client anywhere in the world. IMAP4 includes operations for "creating, deleting, and renaming mailboxes; checking for new messages; permanently removing messages; setting and clearing flags; MIME parsing; searching; and selective fetching of message attributes, texts, and portions thereof."
A list of valid commands is listed below. Note that the implementation of some commands is optional. Some of the arguments to a command may also be optional, such arguments are enclosed in square brackets. 3.2.1 USER name To authenticate using the USER and PASS command combination, the client must first issue the USER command. If the POP3 server responds with a positive status indicator ("+OK"), then the client may issue either the PASS command to complete the authentication.
3.2.2 PASS string When the client issues the PASS command, the POP3 server uses the argument pair from the USER and PASS commands to determine if the client should be given access to the appropriate mailbox. The password is sent in the clear on the network so this method is not good from a security viewpoint. 3.2.3 APOP name digest (Optional) The APOP command provides an alternate method of authentication, which does not involve sending a password in the clear over the network. A POP3 server which implements the APOP command will include a timestamp in its banner greeting. The POP3 client makes note of this timestamp, and then issues the APOP command. The `name' parameter has identical semantics to the `name' parameter of the USER command. The `digest' parameter is calculated by applying the MD5 algorithm to a string consisting of the timestamp followed by the password. 3.2.4 STAT The response consists of the number of messages in the mailbox and the size of the mailbox in octets (bytes). 3.2.5 LIST [msg] The response consists of the message-number of the message and the exact size of the message in octets. If the msg argument (optional) was given then the information is listed for that particular message number, else a multi-line listing containing information for all messages in the mailbox is returned. 3.2.6 RETR msg The POP3 server sends the entire message corresponding to the given message-number. 3.2.7 DELE msg The POP3 server marks the message as deleted. The POP3 server does not actually delete the message until the user gives the QUIT command. 3.2.8 NOOP The POP3 server does nothing, it merely replies with a positive response. 3.2.9 RSET If any messages have been marked as deleted by the POP3 server, they are unmarked. 3.2.10 QUIT The POP3 server removes all messages marked as deleted from the mailbox. The server then closes the TCP connection.
12
Figure
Since a CGI program is executable, it is basically the equivalent of letting the world run a program on your system, which isn't the safest thing to do. Therefore, there are some security precautions that need to be implemented when it comes to using CGI programs. Probably the one that will affect the typical Web user the most is the fact that CGI programs need to reside in a special directory, so that the Web server knows to execute the program rather than just display it to the browser. This directory is usually under direct control of the webmaster, prohibiting the average user from creating CGI programs. The sequence of events happening through CGI scripting is listed as follows: A client makes an HTTP request by means of a URL. This URL could be typed into the location window of a browser, be a hyperlink or be specified in the Action attribute of an HTML form tag. From the URL the web server determines that is should activate the gateway program listed in the URL and send any parameters via the URL to that program. The gateway program processes the information and returns HTML text to the web server. The server in turn adds a MIME header and returns the HTML text to the browser. The web browser displays the document received from the web browser
4.2 How information is transferred from the web browser to a CGI program?
There are two methods, which can be used to access forms. These methods are GET and POST. Depending on which method used, we will receive the encoded results of the form in a different way. The GET method If the form has METHOD="GET" in its FORM tag, the CGI program will receive the encoded form input in the environment variable QUERY_STRING. The value of all the fields are concatenated and passed to the URL specified in the action attribute of the form tag. The basic procedure is to split the data by the ampersands. Each fields values appear in the name-value format. Any character with a special meaning in the forms data is encoded using a special encoding scheme known as URL encoding.
13
The POST method If the form has METHOD="POST" in its FORM tag, the CGI program will receive the encoded form input on stdin. The server will NOT send an EOF on the end of the data; instead you should use the environment variable CONTENT_LENGTH to determine how much data should be read from stdin.
14
other documents. The client must know what kind of document you're sending it so it can present it accordingly. In order for the client to know this, your CGI program must tell the server what type of document it is returning. In order to tell the server what kind of document you are sending back, whether it be a full document or a reference to one, CGI requires you to place a short header on your output. This header is ASCII text, consisting of lines separated by either linefeeds or carriage returns (or both) followed by a single blank line. The output body then follows in whatever native format.
A full document with a corresponding MIME type In this case, you must tell the server what kind of document you will be outputting via a MIME type. Common MIME types are things such as text/html for HTML, and text/plain for straight ASCII text. For example, to send back HTML to the client, your output should read: Content-type: text/html <HTML><HEAD> <TITLE>output of HTML from CGI script</TITLE> </HEAD><BODY> <H1>Sample output</H1> What do you think of <STRONG>this?</STRONG> </BODY></HTML>
A reference to another document Instead of outputting the document, you can just tell the browser where to get the new one, or have the server automatically output the new one for you. For example, say you want to reference a file on your Gopher server. In this case, you should know the full URL of what you want to reference and output something like: Content-type: text/html Location: gopher://httprules.foobar.org/0 <HTML><HEAD> <TITLE>Sorry...it moved</TITLE> </HEAD><BODY> <H1>Go to gopher instead</H1> Now available at <A HREF="gopher://httprules.foobar.org/0">a new location</A> on our gopher server. </BODY></HTML>
15
1. SAMPLE PROGRAM
/* This program checks a username and password against a list in the file (passw.fle in server's root directory) using the format =username&=password. If it is the same, opens index.htm in the directory ok under the server's root directory. If not displays "INVALID ENTRY" and logs the username, password and time into data.txt in the server's root directory. The file passw.fle must be created in the root directory of your server For example =xyz&=abc123. This will allow xyz to use abc123 as his password.The file data.txt will be created when the first invalid entry is entered. */ /* THE HTML FORM FILE looks like below
Password
Send
#include <fcntl.h> #include <conio.h> #include <time.h> #pragma #pragma #pragma #define #define #define hdrstop warn -aus warn -sig strsize 42 EXTRA 5 datafile "../data.txt"
// FUNCTIONS // FUNCTION NoEntre() void NoEntre(char *paswd) { char str[] = " INVALID PASSWORD! "; char str2[] = "UNABLE TO OPEN DATA FILE."; time_t t; FILE *f; //will enter every password entry if it is incorrect. f = fopen(datafile, "a"); // print the CGI response header printf(<html>\n> printf(<head><title>CGI Output</title></head>\n); printf(<body>); //if the file's directory does not exist (the file can not //be created). if(f == NULL) printf("<p><h1>%s </h1></p>\n", str2) ; /*print INVALID PASSWORD! and put the invalid password and time in data.txt */ else { printf("<p><h1>%s </h1></p>\n", str) ; fputs(paswd, f); time(&t); fputs(" ",f); fputs(ctime(&t), f); } printf("</body>\n") ; printf("</html>\n") ; exit(0) ; fclose(f); 17
} // FUNCTION unencode() //Converts the environmental space string to multiple strings and takes out the &, +, =, and %. And adds carriage returns and spaces where needed.// void unencode(char *src, char *last, char *dest) { for(; src != last; src++, dest++) if(*src == '+') *dest = ' '; else if(*src == '&') *dest = '\n'; else if(*src == '&') *dest = ' '; else if(*src == '=') *dest = ' '; else if(*src == '%') { int code; if(sscanf(src+1, "%2x", &code) != 1) code ='?'; *dest = code; src +=2; } else *dest = *src; *dest = '\n'; *++dest = '\0'; } // FUNCTION inputfile() int inputfile(char *pasw, int lensize, char *usern) { //puts the stdin (environment variable) into username. fgets(usern, lensize+1, stdin); unencode(usern, usern+lensize, pasw); return 1; } 18
// FUNCTION readpassw() //checks the password and username against the password and username in the file (passw.fle). int readpasw(char *name) { char buff[60]; char string[60], string2[60]; int d=0; //open file for read ifstream fname("../passw.fle", ios::in || O_RDONLY); if(!fname) return 0; d = strlen(name); string2[0] = '\0'; strncat(string2, name, d); while(fname.getline(buff, sizeof(buff))) { strncpy(string, buff, sizeof(buff)); int e = strcmp(string, string2); if(e != 0) { string[0] = '\0'; buff[0] = '\0'; } else { fname.close(); return 1; } } fname.close(); return 2; } // FUNCTION readfile() //opens ok/index.html void readfile(void) { char filename[] = "../ok/index.htm"; char buffer[80]; 19
FILE *fname; //open file for read if((fname = fopen(filename, "r")) == NULL) { printf("UNABLE TO OPEN //OK//INDEX.HTM"); exit(1); ` } while(!feof(fname)) { fgets(buffer, sizeof(buffer), fname); printf("%s\n", buffer); } fclose(fname); }
// MAIN PROGRAM void main(void) { char *lenstr; //pointer to html string. char username[strsize + 1], password[strsize + 1]; username[0] = '\0'; password[0] = '\0'; long len; //holds the length of the html string. //Find an environment variable and assigns it to the pointer lenstr. lenstr = getenv("CONTENT_LENGTH"); //get the length of the string, if length too little or big runs NoEntre(). if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > (strsize - 1)) NoEntre(username); else { int p = inputfile(password, len, username); if(p == 0) exit(1); 20
int j = readpasw(username); if(j == 0) { printf("\nUNABLE TO OPEN PASSWORD FILE."); exit(1); } if(j == 1) { readfile(); } if(j == 2) { NoEntre(username); } }
21