Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Server Project More tips on How to. Getting started – think of what to do (1) Process configuration –Port –MaxThreads –ScriptAlias –Alias –DocumentRoot.

Similar presentations


Presentation on theme: "Web Server Project More tips on How to. Getting started – think of what to do (1) Process configuration –Port –MaxThreads –ScriptAlias –Alias –DocumentRoot."— Presentation transcript:

1 Web Server Project More tips on How to

2 Getting started – think of what to do (1) Process configuration –Port –MaxThreads –ScriptAlias –Alias –DocumentRoot –ServerRoot –log –Mime.types

3 Getting started – think of what to do (2) Parse HTTP request –Method –URI with query string or arguments –Read in headers –Is Authentication required? –Is caching used? –Is the CGI invoked?

4 Getting started – think of what to do (3) Generate HTTP response –Status code –Create headers –Attach body (optional) Should handle binary properly

5 Getting started – think of how to invoke CGI String[] env = createEnvVariables(); Process proc = Runtime.getRuntime().exec(request.getPath(), env); // For POST send the parameters to STDIN if(request.getMethod().equals("POST")) servePostParameters(proc); BufferedInputStream in = new BufferedInputStream(proc.getInputStream()); // read in the execution results return contentLen; } l.addElement("SERVER_PORT=" + MyServer.htProp.getPort()); … String[] returner = new String[l.size()]; for(int i = 0; i < l.size(); i++) returner[i] = (String)l.elementAt(i); EnvVariables

6 /*** Used for the POST method @param BufferedReader in */ private void readParameterString( BufferedReader in ) throws Exception { StringBuffer buf = new StringBuffer(); // Read characters one by one from the input stream while(in.ready()) buf.append( (char)in.read() ); parameterString = buf.toString(); } // end-readParameterString /*** Used for the GET method */ private void extractParameterString() throws Exception { // Were there any parameters passed? int paramPtr = path.indexOf("?"); if( paramPtr > 0 ) { // Set parameter string parameterString = path.substring( paramPtr + 1 ); // Strip path of the parameters path = path.substring(0, paramPtr); } } // end-extractParameterString______________________________________ How to set Parameter String for GET/POST

7 /** * @param Process p - running CGI process * * Writes the post body message into the input stream of the * running CGI process */ void servePostParameters(Process p) throws Exception { BufferedOutputStream args = new BufferedOutputStream(p.getOutputStream()); String input = request.getParameterString(); for(int i = 0; i < input.length(); i++) args.write((int)input.charAt(i)); args.close(); } // end-servePostParameters How to pass Parameter String to CGI

8 Etc… File file; writer.println("Content-Length: " + (valid ? ((contentLen > -1) ? contentLen : file.length()) : 128)); writer.println("Last-Modified: " + new Date(file.lastModified()));

9 Getting started – think of how to mix PrintWriter & binary stream in the response private void serveFile() throws Exception { byte[] inBuffer = new byte[BUF_LEN]; int bytesRead; FileInputStream in = new FileInputStream(file); try { do { bytesRead = in.read(inBuffer); if(bytesRead > 0) out.write( inBuffer, 0, bytesRead ); } while( bytesRead == BUF_LEN ); } catch( SocketException e ) {} in.close(); out.close(); } OutputStream out; PrintWriter writer = new PrintWriter( out, true ); writer.println(request.getProtocol() + " " + code + " " + reasonPhrase);

10 Multithread example I A server class starts max number threads of request_handler that extends Thread class. for(i=0; i<maxthreads;i++){ request_handler server_thread = new request_handler(ss,i,hc,mc,l); server_thread.start(); } hconfig = new httpd_config("httpd.conf"); if(!hconfig.isValid()){ System.out.println("Configuration file not correct"); return;} mime = new mime_config(hconfig.getValue("TypesConfig")); mime.setDefault(hconfig.getValue("DefaultType")); s = new server(hconfig,mime); if(s.isReady()){ s.run(); } Server.run

11 public void run(){ while(true){ reset(); s = get_socket(); if(s==null){ // got an error, // return and kill thread return; } set_streams(); get_request(); process_request(); send_reply(); close_socket(); } Request_handler.run private void reset(){ env.reset(); r_method = null; r_page = null; r_http_version = null; r_address = ""; r_agent = "-"; r_accept = null; r_referer = "-"; r_ctype = null; r_clength = "0"; r_command = ""; error_code = 0; script = false; filename = null; scriptname = null; filelength = 0; filestream = null; } Request_handler.reset

12 private synchronized Socket get_socket(){ try{ Socket incoming = ss.accept(); return(incoming); } catch(IOException e){ System.out.println(e); } return(null); } Request_handler.get_socket

13 Multithread example II A main server class waits for connections and creates new thread when a new request arrives. The ServerHandler extends thread class. while( true ) { Socket socket = sSocket.accept(); new ServerHandler( socket, numThreadsCreated, srmProp ).start(); } BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); request.readRequest(in); response = new HResponse(request,socket.getOutputStream()); socket.close(); ServerHandler.run

14 Grading Philosophy Grading is based on functionality developed by you. You may develop authentication utilizing instead of.htaccess, develop caching using different header rather than “notModifiedSince” Please specify what is implemented and why you chose not to follow the guidelines in your documentation and explain to TA during the interactive grading

15 Web server Grading Part I Basic functionality (3/4) –Request & response using network connection –Configuration –Images and other mime types –Mandatory session with TA during the week of 3/6 ~ 3/10

16 Web server Grading Part II Advanced functionality (3/18) –Multithreading –Invoking CGI –Caching –Authentication –Persist connection –More features?


Download ppt "Web Server Project More tips on How to. Getting started – think of what to do (1) Process configuration –Port –MaxThreads –ScriptAlias –Alias –DocumentRoot."

Similar presentations


Ads by Google