CGI Lecture 7 cs193i – Internet Technologies Summer 2004 Stanford University.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Advertisements

Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
JavaScript Forms Form Validation Cookies CGI Programs.
How the web works: HTTP and CGI explained
CGI. CGI Programming What is "CGI"? –Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
8/17/2015CS346 PHP1 Module 1 Introduction to PHP.
Common Gateway Interface
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
1 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
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.
Copyright (c) 2010, Dr. Kuanchin Chen1 The Client-Server Architecture of the WWW Dr. Kuanchin Chen.
CP476 Internet Computing CGI1 CGI is a common way to provide for specific computations on server side, interactions with users, or access to databases.
Web Server Design Week 14 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 4/14/10.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
USING PERL FOR CGI PROGRAMMING
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
Lecture 21 Common Gateway Interface CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Chapter 8 Cookies And Security JavaScript, Third Edition.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Website Development with PHP and MySQL Saving Data.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
Chapter 6 Server-side Programming: Java Servlets
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
Week 9 - Form Basics Key Concepts 1. 1.Describe common uses of forms on web pages 2.Create forms on web pages using the form, input, textarea, and select.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Perl: Lecture 2 Advanced RE & CGI. Regular Expressions 2.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
Form Data Encoding GET – URL encoded POST – URL encoded
Introduction to Programming the WWW I CMSC Winter 2003.
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.
Netprog 2002 CGI Programming1 CGI Programming CLIENT HTTP SERVER CGI Program http request http response setenv(), dup(), fork(), exec(),...
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
1 10/19/05CS360 Windows Programming ASP.NET. 2 10/19/05CS360 Windows Programming ASP.NET  ASP.NET works on top of the HTTP protocol  Takes advantage.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Web Server Design Week 13 Old Dominion University Department of Computer Science CS 495/595 Spring 2012 Michael L. Nelson 04/03/12.
Web Server Design Week 15 Old Dominion University Department of Computer Science CS 495/595 Spring 2009 Michael L. Nelson 4/20/09.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
The Common Gateway Interface (CGI) Pat Morin COMP2405.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
How to Write Web Forms By Mimi Opkins.
CGI I: Basics Web Programming.
CGI Programming Part II UNIX Security
Chapter 27 WWW and HTTP.
cs193i – Internet Technologies Summer 2004 Stanford University
The HTTP Protocol COSC 2206 Internet Tools The HTTP Protocol
Web Server Design Week 16 Old Dominion University
CGI I: Basics Web Programming.
Presentation transcript:

CGI Lecture 7 cs193i – Internet Technologies Summer 2004 Stanford University

Administrative Stuff HW #2 due today HW #2 due today HW #3 due August 2 HW #3 due August 2 Midterm should be returned on Monday Midterm should be returned on Monday Final Final Local SCPD students will need to come to campus Local SCPD students will need to come to campus

The Web Platform Web Apps like Google, Amazon, etc... built on the Web "Platform" (as opposed to Win32, Mac, etc...) 1990's, Netscape, Sun, etc... touting the Web Platform Microsoft was not so happy The Browser Wars Today, most OS platforms are Web platform enabled (browser + Java, etc...) VS

Static Pages Retrieve file Send file Request file

Dynamic Pages Do Computation Generate HTML page with results of computation Return dynamically generated HTML file Request service

Server Side Includes (SSI).shtml files Directives embedded in HTML comments Evaluated while page being served Can add dynamically generated content to page Slow

CGI – Common Gateway Interface Invented in 1993 by NCSA for HTTPd web server Client requests program to be run on server-side Web server passes parameters to program through UNIX shell environment variables Program spawned as separate process via fork Program's output => Results Server passes back results (usually in form of HTML) Good for interfacing external applications with information servers See

Competing Technologies CGI & Perl (HW #3) PHP - PHP Hypertext Preprocessor LAMP Architecture (Linux, Apache, MySQL, PHP/Perl/Python) JSP - JavaServer Pages (HW #4) ASP - Active Server Pages

CGI Web Application Run CGI program … print $result Request service HEADERS BODY

Just a Perl Program Write a standard Perl Program Program's output (to stdout) is sent back as HTTP Response You must write out everything Headers Blank Space Body

printenv.pl (Client side) #!/usr/pubsw/bin/perl # $Id: printenv.pl,v /04/13 04:15:36 morpheus Exp $ # printenv.pl -- demo perl program that prints out #environment variables. print "Content-type: text/plain\n\n"; foreach $var (sort(keys(%ENV))) { $val = $ENV{$var}; $val = $ENV{$var}; $val =~ s|\n|\\n|g; $val =~ s|\n|\\n|g; $val =~ s|"|\\"|g; $val =~ s|"|\\"|g; print "${var}=\"${val}\"\n"; print "${var}=\"${val}\"\n";}

elaine35:/usr/class/cs193i/cgi-bin> telnet cgi.stanford.edu 80 Trying Connected to cgi1.Stanford.EDU ( ). Escape character is '^]'. GET /class/cs193i/cgi-bin/printenv.pl HTTP/1.0 HTTP/ OK Date: Wed, 21 Jul :00:33 GMT Server: Apache Connection: close Content-Type: text/plain; charset=ISO DOCUMENT_ROOT="/web/htdocs" GATEWAY_INTERFACE="CGI/1.1" KRB5CCNAME="FILE:/tmp/K5tkt25842class-cs193i.cgi" KRBTKFILE="/tmp/tkt25842class-cs193i.cgi" PATH="/usr/local/bin:/usr/pubsw/bin:/usr/bin:/bin" QUERY_STRING="" REMOTE_ADDR=" " REMOTE_HOST="elaine35.stanford.edu" REMOTE_PORT="46448"

REQUEST_METHOD="GET" REQUEST_URI="/class/cs193i/cgi-bin/printenv.pl" SCRIPT_FILENAME="/afs/ir/class/cs193i/cgi-bin/printenv.pl" SCRIPT_NAME="/~class-cs193i/printenv.pl" SCRIPT_URI=" SCRIPT_URL="/class/cs193i/cgi-bin/printenv.pl" SERVER_ADDR=" " SERVER_NAME="cgi.stanford.edu" SERVER_PORT="80" SERVER_PROTOCOL="HTTP/1.0" SERVER_SOFTWARE="Apache" Connection closed by foreign host.

Client-Side Analysis Nothing new: looks like standard HTTP Request-Response But, actually: Not return printenv.pl file, but rather the output of running that program!!! What if we move the printenv.pl file out of the cgi-bin directory???

elaine35:/usr/class/cs193i/cgi-bin> telnet www 80 Trying Connected to www10.Stanford.EDU ( ). Escape character is '^]'. GET /class/cs193i/printenv.pl HTTP/1.0 HTTP/ OK Date: Wed, 21 Jul :05:09 GMT Server: Apache Last-Modified: Fri, 30 Apr :42:41 GMT ETag: "25f4da82-14f " Accept-Ranges: bytes Content-Length: 335 Connection: close Content-Type: text/plain; charset=ISO Content-Language: en #!/usr/pubsw/bin/perl # $Id: printenv.pl,v /04/13 04:15:36 morpheus Exp $ # printenv.pl -- demo perl program that prints out environment variables. … printenv.pl in WWW directory

What happened? Same File Requested Different Directory Path Different Behaviors! regular directory => returns the file cgi-bin => returns output of the program Which Behavior is determined by Server Based on directory, or file extension,...

Server-Side Request from Client If path in special cgi-bin directory, pass to CGI handler Headers At minimum, Content-type (e.g. Content-type: text/html) Blank Space Body HTML with interspersed output variables Or images, text, pdf,... depends on Content-type Send Results to Client as HTTP Response

Bottom Line Perl/CGI Web App Structure CGI runs on server side Put out HTML/Forms to present data and controls for user to take further actions

To Create Your Very Own CGI files Sign up for CGI capabilities Click on "Activate Personal CGI Service" link Start Writing CGIs! Be careful of Security Issues

Hello World! elaine35:/usr/class/cs193i/cgi-bin> less hello.pl #!/usr/bin/perl -w ## Hello.pl -- demonstrate a trivial CGI that prints ## out some HTML and the current time on this server. use strict 'vars'; my($EOL) = "\015\012"; ## This is a human-readable str of the current time my($nowStr); $nowStr = localtime(); ## This line must be included in the header print "Content-type: text/html$EOL$EOL"; ## Write out the HTML content print " Hello.pl \n"; print " \n"; print " Hello.pl \n"; print "Hello there from CGI-land. It's currently '$nowStr'\n"; print " \n";

HTML Forms Use web page to present choices to user Use web page to present choices to user action=url action=url Specifies URL of CGI that gets data Specifies URL of CGI that gets data Maps response to form element Maps response to form element URL?name1=value1&name2=value2… URL?name1=value1&name2=value2… Data returned to CGI via pairs Data returned to CGI via pairs Funny characters use hexadecimal ASCII representation Funny characters use hexadecimal ASCII representation

HTML Form Structure Form Tag Action Attribute Field Method Attribute Field Input Tags Nested in Form Name & Type (what type of input control) Values / Bindings First name:

HTML Form Structure First name:

After Submit Button

Input Tag Types type=checkbox type=radio type=submit type=image type=hidden (we'll see later!) type=reset

More Input Fields

Small Medium Large

Stoat Goat Weasel

red blue green purple … gray

Getting Input Parameters Input can be submitted via GET or POST Handle input parameters through CGI.pm Perl Module

Passing in Parameters GET Method Bindings show up as UNIX Environment Variables QUERY_STRING Environment variable is the query part (after the ?) POST Method Passed in Content part of the HTTP Request Shows up in CGI Program's stdin

Get vs. Post GET Attr/Val pairs attached after ? + CGI operations can be bookmarked - What happens if user refreshes, or clicks back button? Double Submit! Use only for idempotent operations

Get vs. Post POST Attr/Val pairs attached as Request Body + CGI operations cannot be bookmarked - If user refreshes, or clicks back button, browser may display warning Can use for non-idempotent operations Or idempotent ops with LONG URLs

Continuity Problem (User’s Point of View) Page 1 Page 2 Page 3 Page 4 Server State Added book to cart CC# = XXX Billing address Order submitted & logged

The Illusion of Continuity User thinks that choices made on page 1 are remembered on page 3 However HTTP is Stateless Requests from same user do not necessarily come in adjacent requests

Continuity Problem (Server’s Point of View) Request 1 Request 2

Continuity Problem Resolution Back Button Problem Serial Number Solution – track submitted orders Reconcile Double Submits Add record example May be intentional

Store State Somewhere HTTP is stateless Server Side? Makes Server Really Complicated State per client! Client Side?

“Post-It Notes” Server puts little notes on the client side When client submits the next form, it also (unknowingly) submits these little notes Server reads the notes, remembers who the client is

Technique: Hidden Fields + simple way to store state on client side - what if the client (user) closes browser, returns to your site 30 seconds later? bookmarks your page? enters your site through 3rd party links?

Technique: HTTP Cookies pec.html Server can store bite sized information on client side, telling it which URLs this state is valid for When client requests one of those URLs, it transmits the "cookie" to the server + Site will remember who you are - Privacy?

Cookie Syntax On HTTP response, the server writes: Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure On HTTP requests, the client looks through cookie database, finds all cookies that match the current URL (domain+path), and writes: Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2;...

Cookie Example Client requests a document, and receives in the response: Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov99 23:12:40 GMT When client requests a URL in path "/" on this server, it sends: Cookie: CUSTOMER=WILE_E_COYOTE Client requests a document, and receives in the response: Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ When client requests a URL in path "/" on this server, it sends: Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 Client receives: Set-Cookie: SHIPPING=FEDEX; path=/foo

Cookie Example When client requests a URL in path "/" on this server, it sends: Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 When client requests a URL in path "/foo" on this server, it sends: Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX

Some Details

<<EOT; Perl Syntax Puts raw text into specified string Puts raw text into specified string $string = <<EOT; Stuff More Stuff EOT << EOT marks start of data << EOT marks start of data EOT on line by itself with no whitespace marks end EOT on line by itself with no whitespace marks end

#!/usr/bin/perl -wT # Print out the values of all the environment variables # in an HTML. # Call from the shell or invoke as a CGI script. # HTTP header section print "content-type: text/html\r\n\r\n"; $header = <<EOT; DumpEnv EOT $trailer = <<EOT; EOT # Emit an HTML for all the environment vars # set up for the CGI print $header print " \n"; ## iterate over the keys, but sort them first foreach $key (sort (keys %ENV)) { print " $key = $ENV{$key}\n"; }

elaine35:/usr/class/cs193i/cgi-bin> telnet cgi.stanford.edu 80 Trying Connected to cgi1.Stanford.EDU ( ). Escape character is '^]'. GET /class/cs193i/cgi-bin/dumpenv.pl HTTP/1.0 HTTP/ OK Date: Wed, 21 Jul :22:46 GMT Server: Apache Connection: close Content-Type: text/html; charset=ISO DOCUMENT_ROOT = /web/htdocs GATEWAY_INTERFACE = CGI/1.1 …

CGI.pm Module Object Oriented or Function-Oriented Enables easy parsing of inputs use CGI; $query = new = $query->param; ## all variable names $value = $query->param('color'); ## may be = #query->param("sizes"); ## multi-binding

use CGI; my $q = new CGI; print $q->header("text/html"); # Print out all the key/value pairs.... print " Form Bindings "; print " \n"; print " Your Key/Value Bindings... \n"; print ' '; ## note use of ' to hide " in string $var, = $q->param; foreach $var { $val = $q->param($var); print " \n"; ## one for each row print " $var \n"; ## one for each elt print " $val \n"; print " \n"; } print " \n";

CGI Handling Methods param delete delete_all save url cookie …

Form / HTML Methods start_html end_html startform textfield textarea password_field filefield popup_menu scrolling_list... submit hidden …

Environment Variable Methods user_agent path_info remote_host referer request_method...