A ‘minimal’ web-server We can glean insights about the operation of the HTTP protocol if we create our own web-server.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Client-server practices DSC340 Mike Pangburn. Agenda Overview of client-server development Editing on client (e.g., Notepad) or directly on server (e.g.,
Hypertext Transfer PROTOCOL ----HTTP Sen Wang CSE5232 Network Programming.
PART 6 Application Layer
HTTP HyperText Transfer Protocol. HTTP Uses TCP as its underlying transport protocol Uses port 80 Stateless protocol (i.e. HTTP Server maintains no information.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
1 HTTP – HyperText Transfer Protocol Part 1. 2 Common Protocols In order for two remote machines to “ understand ” each other they should –‘‘ speak the.
Creating WordPress Websites. Creating a site on your computer Local server Local WordPress installation Setting Up Dreamweaver.
The Web The World Wide Web What does it do?.
Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays.
World Wide Web1 Applications World Wide Web. 2 Introduction What is hypertext model? Use of hypertext in World Wide Web (WWW) – HTML. WWW client-server.
Programming project #4 1 CS502 Spring 2006 Programming Project #4 Web Server CS-502 Operating Systems Spring 2006.
Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley, July.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
Definitions, Definitions, Definitions Lead to Understanding.
Introduction to Project 1 Web Client and Server Jan 2006.
CS-3103 & CS-502, Summer 2006 Programming Project #31 Programming Project #3 Web Server CS-3103 & CS-502 Operating Systems.
Project 1 Web client and server EECS 340 Jan 2009.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
Client, Server, HTTP, IP Address, Domain Name. Client-Server Model Client Bob Yahoo Server yahoo.com/finance.html A text file named finance.html.
PL-IV- Group A HTTP Request & Response Header
Computer Concepts 2014 Chapter 7 The Web and .
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
HTTP; The World Wide Web Protocol
Agenda  Terminal Handling in Unix File Descriptors Opening/Assigning & Closing Sockets Types of Sockets – Internal(Local) vs. Network(Internet) Programming.
Assignment 3 A Client/Server Application: Chatroom.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
FTP (File Transfer Protocol) & Telnet
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
HyperText Transfer Protocol (HTTP).  HTTP is the protocol that supports communication between web browsers and web servers.  A “Web Server” is a HTTP.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
2: Application Layer1 CS 4244: Internet Software Development Dr. Eli Tilevich.
Rensselaer Polytechnic Institute Shivkumar Kalvanaraman, Biplab Sikdar 1 The Web: the http protocol http: hypertext transfer protocol Web’s application.
Maryam Elahi University of Calgary – CPSC 441.  HTTP stands for Hypertext Transfer Protocol.  Used to deliver virtually all files and other data (collectively.
© Janice Regan, CMPT 128, Jan 2007 CMPT 371 Data Communications and Networking HTTP 0.
A Closer Look at HTTP HyperText Transfer Protocol.
WebServer A Web server is a program that, using the client/server model and the World Wide Web's Hypertext Transfer Protocol (HTTP), serves the files that.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
CS 241 Section (04/19/12). MP8  Web Server  Due: Tuesday, May 1 st, 11:59pm  What will you be doing?  Creating a web-server in C that serves HTML.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
1 Welcome to CSC 301 Web Programming Charles Frank.
Proxy Lab Recitation I Monday Nov 20, 2006.
Lab 2 Primer Assignment 3 Structure File I/O More parsing and HTTP Formatting.
1-1 HTTP request message GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr request.
WWW: an Internet application Bill Chu. © Bei-Tseng Chu Aug 2000 WWW Web and HTTP WWW web is an interconnected information servers each server maintains.
WEB SERVER Mark Kimmet Shana Blair. The Project Web Server Application  Receives request for web pages or images from a client browser via the internet.
Jan 2001C.Watters1 World Wide Web and E-Commerce Client Side Processing.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications  app architectures  app requirements r 2.2 Web and HTTP.
CITA 310 Section 2 HTTP (Selected Topics from Textbook Chapter 6)
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Overview of Servlets and JSP
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 7 Omar Meqdadi Department of Computer Science and Software Engineering University of.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Week 11: Application Layer 1 Web and HTTP r Web page consists of objects r Object can be HTML file, JPEG image, Java applet, audio file,… r Web page consists.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
© Janice Regan, CMPT 128, Jan 2007 CMPT 371 Data Communications and Networking HTTP 0.
Tiny http client and server
Assignment 3 A Client/Server Application: Chatroom
HTTP – An overview.
The Hypertext Transfer Protocol
Client/Server Example
Client side & Server side scripting
HTTP Hypertext Transfer Protocol
Introduction to Client/Server Design
Traditional Internet Applications
HTTP Hypertext Transfer Protocol
Presentation transcript:

A ‘minimal’ web-server We can glean insights about the operation of the HTTP protocol if we create our own web-server

A web-browser is our ‘client’ Mozilla FirefoxOur ‘mini’ server HTTP request line Request ‘headers’ Optional ‘body’ Request-message (to server) HTTP status line Response ‘headers’ Optional ‘body’ Response-message (to client)

TCP server ‘boilerplate’ By now we have seen that any connection oriented ‘server’ has some standard steps to execute by way of ‘connection-setup’ –Initialize a host ‘socket-address’ structure –Create a TCP socket to use for ‘listening’ –Bind the ‘socket-address’ to this socket –Enable the socket to ‘reuse’ its port-number –Convert the socket into a ‘listening’ socket –Accept a ‘connection-request’ from a client

‘helper’ functions We can employ standard library-functions to help setup the internet socket-address data-structure for our server’s host: –‘gethostname()’ –‘gethostbyname()’ –‘inet_ntoa()’ –‘inet_addr()’ –‘htons()’

Our ‘sockaddr_in’ setup padded with zeros sin_familysin_portsin_addr struct sockaddr_in

Setup our ‘listening’ socket

‘iterative’ server-design ‘accept’ a connection-request from a client ‘receive’ the HTTP request-message validate and process the HTTP command ‘send’ the HTTP response-message ‘close’ the connection with this client We use some new ‘helper’ functions for these steps

‘fdopen()’ To conveniently process the incoming stream of characters in a client’s HTTP request-message (which is organized into lines of text with CRLF as the line-delimiter), and to conveniently reply with similarly organized lines of text, we would like to use the standard ‘buffered’ i/o functions To do this requires substituting ‘streams’ for our TCP ‘connection’ socket, which is accomplished by using the ‘fdopen()’ library-function

‘fdopen()’ and ‘dup()’ To conveniently process the incoming stream of characters in a client’s HTTP request-message (which is organized into lines of text with CRLF as the line-delimiter), and to conveniently reply with similarly organized lines of text, we would like to use the standard ‘buffered’ i/o functions To do this will require us to associate ‘streams’ with our TCP ‘connection’ socket, one for input, one for output, which we can accomplish using the ‘fdopen()’ and ‘dup()’ library-functions

Our i/o streams Our connection-socket is named ‘conn’: We associate an input-stream with ‘conn’: And we associate an output-stream with a ‘duplicate’ of our connection-socket ‘conn’: struct sockaddr_incaddr = { 0 }; socklen_tsalen = sizeof( caddr ); intconn = accept( sock, (sockaddr*)&caddr, &calen ); FILE*rx = fdopen( conn, “r” ); FILE*tx = fdopen( dup( conn ), “w” );

Request message’s lines Now it’s easy to receive the first ‘line’ of an HTTP request-message sent by our client: The remaining lines (request headers and the empty line) can be received like this: charrequest[ BUFSIZ ] = { 0 }; fgets( request, BUFSIZ, rx ); charheader[ BUFSIZ ] = { 0 }; while ( fgets( request, BUFSIZ, rx ) ) if ( strcmp( header, “\r\n” ) == 0 ) break;

Parsing the ‘request’ The HTTP Request-line is comprised of three fields, separated by a blank-space and terminated with the CR/LF delimiter We can use the formatted string-scan function ‘sscanf()’ to parse the request ”\r\n” charcmd[ BUFSIZ ] = { 0 }, arg[ BUFSIZ ] = { 0 }; strcpy( arg, “./” );// for restricting the client’s access if ( sscanf( “%s%s”, cmd, arg+2 ) != 2 ) { perror( “sscanf” ); }

Validating the request For now our ‘miniature’ web-server only accepts the basic HTTP ‘GET’ command: if ( strcmp( cmd, “GET” ) != 0 ) { // the HTTP Response status, headers, and empty line fprintf( tx, “HTTP/ Not Implemented \r\n” ); fprintf( tx, “Content-type: text/plain \r\n” ); fprintf( tx, “\r\n” ); // the HTTP Response-message’s ‘body’ fprintf( tx, “That one is not yet implemented \r\n” fclose( rx ); fclose( tx ); continue; }

‘nonexistent’ resource? The ‘stat()’ library-function can be used to verify that a requested resource exists struct statinfo = { 0 }; if ( stat( arg, &info ) < 0 )// the target does not exist { // the HTTP Response’s status, headers, and empty line fprintf( tx, “HTTP/ Not Found \r\n” ); fprintf( tx, “Content-type: text/plain \r\n” ); fprintf( tx, “\r\n” ); // the HTTP Response-message’s ‘body’ fprintf( tx, “Requested item not found \r\n” fclose( rx ); fclose( tx ); continue; }

‘struct stat’

‘statdemo()’ We have posted a demo-program which lets you see the information about a file which the Linux operating system keeps track of (including the ‘type’ of the file) EXAMPLE: $./statdemo mywebsvr.cpp

‘folder’ or ‘file’? Our server can send its client a directory- listing if that type of resource is requested if ( S_ISDIR( info.st_mode ) ) // the requested resource is a directory { // the HTTP Response’s status, headers, and empty line fprintf( tx, “HTTP/ OK \r\n” ); fprintf( tx, “Content-type: text/plain \r\n” ); fprintf( tx, “\r\n” ); // the HTTP Response-message’s ‘body’ fflush( tx ); if ( fork() == 0 ) // child-process will deliver the directory-listing { dup2( conn, 1 ); dup2( conn, 2 ); close( conn ); execlp( “ls”, “ls”, “-l”, arg, NULL ); exit( 1 ); } wait( NULL ); // parent-process will sleep until tge child exits fclose( rx ); fclose( tx ); continue; }

A sample web-page We created a tiny webpage example for demonstrating our web-server’s ability to deliver an ‘HTML’ file to a web-browser

Delivering an ‘html’ file if ( strcmp( strrchr( arg, ‘.’ ) + 1, “html” ) == 0 ) // the requested resource is an ‘html’ file { // the HTTP Response’s status, headers, and empty line fprintf( tx, “HTTP/ OK \r\n” ); fprintf( tx, “Content-type: text/html \r\n” ); fprintf( tx, “\r\n” ); // the HTTP Response-message’s ‘body’ fflush( tx ); if ( fork() == 0 ) // child-process will deliver the file’s contents { dup2( conn, 1 ); dup2( conn, 2 ); close( conn ); execlp( “cat”, “cat”, arg, NULL ); exit( 1 ); } wait( NULL ); // parent-process will sleep until the child exits fclose( rx ); fclose( tx ); continue; }

‘mywebsvr.cpp’ We posted this miniature web-server on our course website for you to download You can use Linux’s ‘Firefox’ browser to see if indeed it can deliver the sample HTML-file (named ‘mywebpage.html’) First start the web-server application, then launch ‘Firefox’ and type in this URL: Your classroom host’s station-number

In-class exercise #1 Try using Microsoft’s Internet Explorer to request the ‘mywebpage.html’ document Edit your copy of ‘mywebpage.html’ to add the following attributes within the tag in that HTML document:

In-class exercise #2 Copy the file named ‘warrior.jpg’ from our course website into your local directory Then modify the ‘mywebpage.html’ file by adding this extra line in front of : Now extend the functionality of our ‘mini’ web-server by adding a block of new code which enables a child-process to deliver an image-file having the ‘.jpg’ file-suffix

Exercise #2 (continued) You can do this with your Editor by using ‘copy-and-paste’ applied to the code-block that delivers an ‘html’ file to a client – just change the ‘html’ file-suffix to ‘jpg’, and change the ‘Content-type’ statement so that ‘text/html’ becomes ‘image/jpeg”