Web-fejlesztés NGM_IN002_1 CGI, SSI
Dinamikus tartalom el!állítás Webes prezentáció és átvitel HTML, (MIME), HTTP Megvalósítás Alkalmazás natív webes interfésszel Standard webszerver funkcionalitásának kiterjesztése Webes interfész Humán interfész böngész!, HTML Nehézségek tartalom <-> megjelenítés teljesítmény => használjunk önálló webréteget
field field OK Text Field Text Field Browser Cancel Common Gateway Interface Interfész specifikáció a webszerver és az általa elindított alkalmazás adat cseréjére Database Browser request result Webserver CGI program web gateway SQL receive process response HTTP CGI Egyéb interfészek CGI alkalmazások "rlapok (forms) feldolgozása interakció Gateway funkciók webes interfész Virtuális dokumentumok dinamikus tartalom
A CGI m#ködése HTTP kérés a webszerverhez GET, POST CGI élet ciklus (single request) új process létrehozása a CGI számára a kérés paramétereinek elérhet!vé tétele a CGI program számára szerver és op. rendszer függ! stdio, környezeti változók, stb. válasz visszaadása CGI programozási környezetek A script nyelvek megfelel!ek CGI-k írására jó szöveg manipulációs képességek együttm#ködés más programokkal CGI környezet elérése hordozhatóság de teljesítménykorlátok Szokásos CGI környezetek Shell scriptek (UNIX) Perl (UNIX, Mac OS, Windows) C
Az interfész Environment Variable Description GATEWAY_INTERFACE SERVER_NAME SERVER_SOFTWARE SERVER_PROTOCOL SERVER_PORT REQUEST_METHOD PATH_INFO PATH_TRANSLATED SCRIPT_NAME DOCUMENT_ROOT The revision of the Common Gateway Interface that the server uses. The server's hostname or IP address. The name and version of the server software that is answering the client request. The name and revision of the information protocol the request came in with. The port number of the host on which the server is running. The method with which the information request was issued. Extra path information passed to a CGI program. The translated version of the path given by the variable PATH_INFO. The virtual path (e.g., /cgi-bin/program.pl) of the script being executed. The directory from which Web documents are served. QUERY_STRING The query information passed to the program. It is appended to the URL with a "?". REMOTE_HOST REMOTE_ADDR AUTH_TYPE REMOTE_USER REMOTE_IDENT CONTENT_TYPE CONTENT_LENGTH HTTP_FROM HTTP_ACCEPT HTTP_USER_AGENT HTTP_REFERER The remote hostname of the user making the request. The remote IP address of the user making the request. The authentication method used to validate a user. The authenticated name of the user. The user making the request. The MIME type of the query data, such as "text/html". The length of the data (in bytes or the number of characters) passed to the CGI program through standard input. The email address of the user making the request. Most browsers do not support this variable. A list of the MIME types that the client can accept. The browser the client is using to issue the request. The URL of the document that the client points to before accessing the CGI program. Formok feldolgozása GET POST URL-encoded Content-Type: application/x-www-form-urlencoded HTTP kérés feldolgozása CGI felismerése elérési könyvtár meghatározása végrehajtási jog ellen!rzése a könyvtárra fájlrendszer végrehajtási jogok ellen!rzése (UNIX) környezeti változók beállítása process indítás CGI program kilépése után a tartalom kiszolgálás, a fejléc mez!k kiegészítése
Form adatok elérése Query string URL (GET) QUERY_STRING környezeti vált. Üzenet testben (POST) stdin CONTENT_LENGTH REQUEST_METHOD parszer kell form mez!k URL encoded data key-value pairs & mez! szeparátor speciális karakterek A CGI program válasza Fejléc mez!k Header Content-length Content-type Expires Location Pragma Status Refresh Set-Cookie Description The length (in bytes) of the output stream. Implies binary data. The MIME content type of the output stream. Date and time when the document is no longer valid and should be reloaded by the browser. Server redirection (cannot be sent as part of a complete header). Turns document caching on and off. Status of the request (cannot be sent as part of a complete header). Client reloads specified document. Client stores specified data. Useful for keeping track of data between requests.
Multimédia tartalom CGI visszatérési értéke multimédia tartalom formátum segéd könyvtárak pl. GD küls! alkalmazások gnuplot FastCGI Nyelvfüggetlen szerver kiterjesztés (modulok) CGI interfész processz-kezelési overhead elkerülése hosszú élet# alkalmazások konkurrens, eseményvezérelt, többszálú m#ködés listening socket
field field OK Text Field Text Field Browser Cancel Server Side Include Korai HTML esetén nem volt mód küls! információ becsatolására csak img elemmel ágyazható be kliens oldalon tartalom Szerver által kiegészített html fájl html w/ssi request Webserver Browser response SSI processing CGI program HTTP SSI virtual include echo SSI beágyazások Command Parameter Description var Inserts value of special SSI variables as well as other environment variables include Inserts text of document into current file file virtual Pathname relative to current directory Virtual path to a document on the server fsize file Inserts the size of a specified file flastmod exec file cmd Inserts the last modification date and time for a specified file Executes external programs and inserts output in current document Any application on the host cgi CGI program
Egyszer# CGI-k #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<HTML><BODY><H1>Hello, World!</H1></BODY><HTML>"; #!/usr/bin/perl use CGI qw(:all); # CGI.pm module is very useful! print header, start_html, h1("hello, World!"), end_html; CGI környezeti változók #!/usr/bin/perl print "Content-type: text/html", "\n\n"; print "<HTML>", "\n"; print "<HEAD><TITLE>About this Server</TITLE></HEAD>", "\n"; print "<BODY><H1>About this Server</H1>", "\n"; print "<HR><PRE>"; print "Server Name: ", $ENV{'SERVER_NAME'}, "\n"; print "Running on Port: ", $ENV{'SERVER_PORT'}, "\n"; print "Server Software: ", $ENV{'SERVER_SOFTWARE'}, "\n"; print "Server Protocol: ", $ENV{'SERVER_PROTOCOL'}, "\n"; print "CGI Revision: ", $ENV{'GATEWAY_INTERFACE'}, "\n"; print "</PRE><HR>", "\n"; print "</BODY></HTML>", "\n";
HTTP kérés és válasz 127.000.000.001.49284-127.000.000.001.00080: GET /cgi-bin/mult.cgi?m=5&n=7 HTTP/1.1 Host: 127.0.0.1 Connection: keep-alive Referer: http://127.0.0.1/~heckenasttamas/mult.html User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.5.6 (KHTML, like Gecko) Safari/125.12 If-Modified-Since: Sat, 26 Feb 2005 10:06:58 GMT Accept: */* Accept-Encoding: gzip, deflate;q=1.0, identity;q=0.5, *;q=0 Accept-Language: en, ja;q=0.82, hu;q=0.93, fr;q=0.89, ja-jp;q=0.86, hu-hu;q=0.96, de-de;q=0.79, de;q=0.75, nl-nl;q=0.71, nl;q=0.68, es;q=0.64, it-it;q=0.61, it;q=0.57, sv-se;q=0.54, sv;q=0.50, no-no;q=0.46, no;q=0.43, da-dk;q=0.39, da;q=0.36 127.000.000.001.00080-127.000.000.001.49284: HTTP/1.1 200 OK Date: Sat, 26 Feb 2005 10:11:57 GMT Server: Apache/1.3.33 (Darwin) Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/html;charset=iso-8859-1 93 <HTML> <HEAD> <TITLE>Multiplication results</title> </HEAD> <BODY> <H3>Multiplication results</h3> <P>The product of 5 and 7 is 35.</BODY> </HTML> 0 Form mez!k parszolása @key_value_pairs = split (/&/, $query_string); foreach $key_value (@key_value_pairs) { ($key, $value) = split (/=/, $key_value); $value =~ tr/+/ /; $value =~ s/%([\da-fa-f][\da-fa-f])/pack ("C", hex ($1))/eg;
Grafikus tartalom #!/usr/local/bin/perl use GD; print "Content-type: image/png\n\n"; # create a new image $im = new GD::Image(100,100); # allocate some colors $white = $im->colorallocate(255,255,255); $black = $im->colorallocate(0,0,0); $red = $im->colorallocate(255,0,0); $blue = $im->colorallocate(0,0,255); # make the background transparent and interlaced $im->transparent($white); $im->interlaced('true'); # Put a black frame around the picture $im->rectangle(0,0,99,99,$black); # Draw a blue oval $im->arc(50,50,95,75,0,360,$blue); # And fill it with red $im->fill(50,50,$red); binmode STDOUT; # Convert the image to PNG and print it on standard output print $im->png; SSI <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN"> <html>! <head>!! <title>ssi test</title>! </head>! <body>! <!--#exec cgi="/cgi-bin/ssitest.cgi"-->! <!--#echo var="date_local"--><br>! <!--#include virtual="ssit.txt"-->! </body> </html>