Netprog 2002 CGI Programming1 CGI Programming CLIENT HTTP SERVER CGI Program http request http response setenv(), dup(), fork(), exec(),...

Slides:



Advertisements
Similar presentations
Hypertext Transfer PROTOCOL ----HTTP Sen Wang CSE5232 Network Programming.
Advertisements

CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Lecture 14 HTML Forms CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
JavaScript Forms Form Validation Cookies CGI Programs.
HTTP Hypertext Transfer Protocol. HTTP messages HTTP is the language that web clients and web servers use to talk to each other –HTTP is largely “under.
Pass data1 Passing data from an HTML page to a program Dr Jim Briggs.
CS 898N – Advanced World Wide Web Technologies Lecture 6: PERL and CGI Chin-Chih Chang
Python and Web Programming
USER INTERACTIONS: FORMS
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
Lecture 13 Dynamic Web Servers & Common Gateway Interface CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Emlékeztető December 1.-én pótoljuk az elmaradt előadást (nov. 24-ről) az A/1 228-as teremben ig December 8.-án tartjuk a pótZH-t a sikertelen ZH-t.
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.
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.
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.
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Internet / Intranet CIS-536 Class 7. 2 HTML Forms A Method to Allow Users to Pass Information to a CGI Script Forms Allow Information to Be Entered Via:
HyperText Transfer Protocol (HTTP).  HTTP is the protocol that supports communication between web browsers and web servers.  A “Web Server” is a HTTP.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
USING PERL FOR CGI PROGRAMMING
Lecture 5 Dynamic Web Servers CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 In the good old days... Years ago… the WWW was made up of (mostly) static documents. –Each URL corresponded to a single file stored on some hard disk.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking.
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
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.
CGI Programming. What is it? CGI –Common Gateway Interface Standard way to pass information back to the Web Server –GET Query String –POST Standard Input.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
Form Data Encoding GET – URL encoded POST – URL encoded
Appendix E: Overview of HTTP ©SoftMoore ConsultingSlide 1.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
 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.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
World Wide Web Components Browsers and Servers CGI Processing Model (Common Gateway Interface) © Norman White, 2001.
Netprog CGI and Forms1 CGI and Forms A detailed look at HTML forms.
HTML Forms. A form is simply an area that can contain form fields. Form fields are objects that allow the visitor to enter information - for example text.
Lecture 19 Overview. Hyper Text Transfer Protocol HTTP is the protocol that supports communication between web browsers and web servers. – A “Web Server”
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.
Pass data1 Passing data from an HTML page to a program Dr Jim Briggs.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
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)
The Common Gateway Interface (CGI) Pat Morin COMP2405.
How to Write Web Forms By Mimi Opkins.
Introduction to CGI and ajax
Introducing Forms.
CGI Programming Part II UNIX Security
Introduction to CGI and ajax
HTTP Hypertext Transfer Protocol
HTTP Hypertext Transfer Protocol
Web Forms.
Presentation transcript:

Netprog 2002 CGI Programming1 CGI Programming CLIENT HTTP SERVER CGI Program http request http response setenv(), dup(), fork(), exec(),...

Netprog 2002 CGI Programming2 Common Gateway Interface CGI is a standard mechanism for: –Associating URLs with programs that can be run by a web server. –A protocol (of sorts) for how the request is passed to the external program. –How the external program sends the response to the client.

Netprog 2002 CGI Programming3 CGI URLs There is some mapping between URLs and CGI programs provided by a web sever. The exact mapping is not standardized (web server admin can set it up). Typically: –requests that start with /CGI-BIN/, /cgi-bin/ or /cgi/, etc. refer to CGI programs (not to static documents).

Netprog 2002 CGI Programming4 Request CGI program The web server sets some environment variables with information about the request. The web server fork() s and the child process exec() s the CGI program. The CGI program gets information about the request from environment variables.

Netprog 2002 CGI Programming5 STDIN, STDOUT Before calling exec(), the child process sets up pipes so that stdin comes from the web server and stdout goes to the web server. In some cases part of the request is read from stdin. Anything written to stdout is forwarded by the web server to the client.

Netprog 2002 CGI Programming6 HTTP SERVER CGI Program stdin stdout Environment Variables

Netprog 2002 CGI Programming7 Important CGI Environment Variables REQUEST_METHOD QUERY_STRING CONTENT_LENGTH

Netprog 2002 CGI Programming8 Request Method: Get GET requests can include a query string as part of the URL: GET /cgi-bin/finger?hollingd HTTP/1.0 Request Method Resource Name Delimiter Query String

Netprog 2002 CGI Programming9 /cgi-bin/finger?hollingd The web server treats everything before the ‘?’ delimiter as the resource name In this case the resource name is the name of a program. Everything after the ‘?’ is a string that is passed to the CGI program.

Netprog 2002 CGI Programming10 Simple GET queries - ISINDEX You can put an tag inside an HTML document. The browser will create a text box that allows the user to enter a single string. If an ACTION is specified in the ISINDEX tag, when the user presses Enter, a request will be sent to the server specified as the ACTION.

Netprog 2002 CGI Programming11 ISINDEX Example Enter a string: Press Enter to submit your query. If you enter the string “blahblah”, the browser will send a request to the http server at foo.com that looks like this: GET /search.cgi?blahblah HTTP/1.1

Netprog 2002 CGI Programming12 What the CGI sees The CGI Program gets REQUEST_METHOD using getenv : char *method; method = getenv(“REQUEST_METHOD”); if (method==NULL) … /* error! */

Netprog 2002 CGI Programming13 Getting the GET If the request method is GET: if (strcasecmp(method,”get”)==0) The next step is to get the query string from the environment variable QUERY_STRING char *query; query = getenv(“QUERY_STRING”);

Netprog 2002 CGI Programming14 Send back http Response and Headers: The CGI program can send back a http status line : printf(“HTTP/ OK\r\n”); and headers: printf(“Content-type: text/html\r\n”); printf(“\r\n”);

Netprog 2002 CGI Programming15 Important! A CGI program doesn’t have to send a status line (the http server will do this for you if you don’t). A CGI program must always send back at least one header line indicating the data type of the content (usually text/html ). The web server will typically throw in a few header lines of it’s own ( Date, Server, Connection ).

Netprog 2002 CGI Programming16 Simple GET handler int main() { char *method, *query; method = getenv(“REQUEST_METHOD”); if (method==NULL) … /* error! */ query = getenv(“QUERY_STRING”); printf(“Content-type: text/html\r\n\r\n”); printf(“ Your query was %s \n”, query); return(0); }

Netprog 2002 CGI Programming17 URL-encoding Browsers use an encoding when sending query strings that include special characters. –Most nonalphanumeric characters are encoded as a ‘%’ followed by 2 ASCII encoded hex digits. –‘=‘ (which is hex 3D) becomes “%3D” –‘&’ becomes “%26”

Netprog 2002 CGI Programming18 More URL encoding The space character ‘ ‘ is replaced by ‘+’. –Why? (think about project 2 parsing…) The ‘+’ character is replaced by “%2B” Example: “foo=6 + 7” becomes “foo%3D6+%2B+7”

Netprog 2002 CGI Programming19 Security!!! It is a very bad idea to build a command line containing user input! What if the user submits: “ ; rm -r *; ” grep ; rm -r *; /usr/dict/words

Netprog 2002 CGI Programming20 Beyond ISINDEX - Forms Many Web services require more than a simple ISINDEX. HTML includes support for forms: –lots of field types –user answers all kinds of annoying questions –entire contents of form must be stuck together and put in QUERY_STRING by the Web server.

Netprog 2002 CGI Programming21 Form Fields Each field within a form has a name and a value. The browser creates a query that includes a sequence of “ name=value” substrings and sticks them together separated by the ‘&’ character.

Netprog 2002 CGI Programming22 Form fields and encoding 2 fields - name and occupation. If user types in “Dave H.” as the name and “none” for occupation, the query would look like this: “name=Dave+H%2E&occupation=none”

Netprog 2002 CGI Programming23 HTML Forms Each form includes a METHOD that determines what http method is used to submit the request. Each form includes an ACTION that determines where the request is made.

Netprog 2002 CGI Programming24 An HTML Form Name: Occupation:

Netprog 2002 CGI Programming25 What a CGI will get The query (from the environment variable QUERY_STRING) will be a URL-encoded string containing the name,value pairs of all form fields. The CGI must decode the query and separate the individual fields.

Netprog 2002 CGI Programming26 HTTP Method: POST The HTTP POST method delivers data from the browser as the content of the request. The GET method delivers data (query) as part of the URI.

Netprog 2002 CGI Programming27 GET vs. POST When using forms it’s generally better to use POST: – there are limits on the maximum size of a GET query string (environment variable) –a post query string doesn’t show up in the browser as part of the current URL.

Netprog 2002 CGI Programming28 HTML Form using POST Set the form method to POST instead of GET. The browser will take care of the details...

Netprog 2002 CGI Programming29 CGI reading POST If REQUEST_METHOD is a POST, the query is coming in STDIN. The environment variable CONTENT_LENGTH tells us how much data to read.

Netprog 2002 CGI Programming30 Possible Problem char buff[100]; char *clen = getenv(“CONTENT_LENGTH”); if (clen==NULL) /* handle error */ int len = atoi(clen); if (read(0,buff,len)<0) … /* handle error */ pray_for(!hacker);

Netprog 2002 CGI Programming31 CGI Method summary GET: –REQUEST_METHOD is “GET” –QUERY_STRING is the query POST: –REQUEST_METHOD is “POST” –CONTENT_LENGTH is the size of the query (in bytes) –query can be read from STDIN

Netprog 2002 CGI Programming32 Form CGI Example Student enters first name, last name and social security number and presses a submit button. CGI program looks up grades for the student and returns a list of grades. Complete example is on the course Web.

Netprog 2002 CGI Programming33 There’s More to Come Keeping track of state information. Cookies. Using HTML templates Using JavaScript to perform form validation and other fancy stuff. Image Mapping Authentication Encryption