Thursday, November 13, 2008

Web Standards

< img src="image.png" >< br >

< div align="center" >text< /div >

< /body >

< /html >

If Apache is serving that, and Firefox picks it up, It will replace the < img src... etc with the image found at image.png relative to the working directory of the page requested, (ie ./, current dir), and the < div... is turned into text printed in the middle of the page. Since the code is processed from top to bottom, the br means that the browser should skip down one line and start the rest from there. The top two and bottom two lines tell the browser what part of the page it is reading. You migh have noticed the < /div >, the < /body >, etc. They "close" the tag. Tag is a term for anything in <>s, and they must be opened (ie introduced) and closed (ie < /tag >). If you want to learn HTML tagging, just head over to our close friend Google and do a search.

Since you haven't gotten to the programming section, and currently I have not even wrote it, I will show you a web server example in the simplest form I can think of that will work on any OS you are currently using. So the obvious choice is JAVA:

import java.net.*; import java.io.*; import java.util.*;

public class jhttp extends Thread {

Socket theConnection;

static File docroot;

static String indexfile = "index.html";

public jhttp(Socket s) {

theConnection = s;

}

public static void main(String[] args) {

int thePort;

ServerSocket ss;

// get the Document root

try {

docroot = new File(args[0]);

}

catch (Exception e) {

docroot = new File(".");

}

// set the port to listen on

try {

thePort = Integer.parseInt(args[1]);

if (thePort < 0 || thePort > 65535) thePort = 80;

}

catch (Exception e) {

thePort = 80;

}

try {

ss = new ServerSocket(thePort);

System.out.println("Accepting connections on port "

+ ss.getLocalPort());

System.out.println("Document Root:" + docroot);

while (true) {

jhttp j = new jhttp(ss.accept());

j.start();

}

}

catch (IOException e) {

System.err.println("Server aborted prematurely");

}

}

public void run() {

String method;

String ct;

String version = "";

File theFile;

try {

PrintStream os = new PrintStream(theConnection.getOutputStream());

DataInputStream is = new DataInputStream(theConnection.getInputStream());

String get = is.readLine();

StringTokenizer st = new StringTokenizer(get);

method = st.nextToken();

if (method.equals("GET")) {

String file = st.nextToken();

if (file.endsWith("/")) file += indexfile;

ct = guessContentTypeFromName(file);

if (st.hasMoreTokens()) {

version = st.nextToken();

}

// loop through the rest of the input li

// nes

while ((get = is.readLine()) != null) {

if (get.trim().equals("")) break;

}

try {

theFile = new File(docroot, file.substring(1,file.length()));

FileInputStream fis = new FileInputStream(theFile);

byte[] theData = new byte[(int) theFile.length()];

// need to check the number of bytes rea

// d here

fis.read(theData);

fis.close();

if (version.startsWith("HTTP/")) { // send a MIME header

os.print("HTTP/1.0 200 OKrn");

Date now = new Date();

os.print("Date: " + now + "rn");

os.print("Server: jhttp 1.0rn");

os.print("Content-length: " + theData.length + "rn");

os.print("Content-type: " + ct + "rnrn");

} // end try

// send the file

os.write(theData);

os.close();

} // end try

catch (IOException e) { // can't find the file

if (version.startsWith("HTTP/")) { // send a MIME header

os.print("HTTP/1.0 404 File Not Foundrn");

Date now = new Date();

os.print("Date: " + now + "rn");

os.print("Server: jhttp 1.0rn");

os.print("Content-type: text/html" + "rnrn");

}

os.println("< HTML >< HEAD >< TITLE >File Not Found< /TITLE >< /HEAD >");

os.println("< BODY >< H1 >HTTP Error 404: File Not Found< /H1 >< /BODY >< /HTML >");

os.close();

}

}

else { // method does not equal "GET" if (version.startsWith("HTTP/")) { // send a MIME header os.print("HTTP/1.0 501 Not Implementedrn"); Date now = new Date(); os.print("Date: " + now + "rn"); os.print("Server: jhttp 1.0rn"); os.print("Content-type: text/html" + "rnrn"); }

os.println("< HTML >< HEAD >< TITLE >Not Implemented< /TITLE >"); os.println("< BODY >< H1 >HTTP Error 501: Not Implemented< /H1 >

0 comments: