Transcript Document

HTTP
HyperText Transfer Protocol
Plāns
• Internet & Web
• Arhitektūra
• HTTP protokols
• Web roboti
Internet & Web
•
The Internet is a worldwide, publicly accessible
series of interconnected computer networks that
transmit data by packet switching using the standard
Internet Protocol (IP)
•
The World Wide Web (commonly
shortened to the Web) is a system of
interlinked, hypertext documents
accessed via the Internet
•
Web application is an application that is accessed
via web over a network such as the Internet or an
intranet
Arhitektūra
• Dalīta klienta servera arhitektūra
• Saziņa notiek pa
datortīklu
• Datortīklu arhitektūras
abstraktais apraksts –
TCP/IP modelis
(Internet reference model)
TCP/IP modelis
IP = Internet Protocol
TCP = Transmission Control Protocol
Lietojumprogrammu līmenis
• Piedāvā lietotājam noderīgo protokolu
kopumu
• Klienta servera sistēmu piemēri un to
lietojumprogrammu līmeņa protokoli:
•
•
•
•
The World Wide Web (HTTP)
E-mail (SMTP & POP)
Reliable file transfer (FTP)
Internet Domain Name System (DNS)
Web klienti un Web serveri
• Web klientu parasti sauc par user agent un
tipiskais piemērs ir pārlūkprogramma (browser)
•
Internet Explorer, Firefox, Opera, Netscape
• Web serveris ir programma, kas ir atbildīga par
klientu pieprasījumu saņemšanu un atbildes
aizsūtīšanu atpakaļ
•
Apache, Microsoft Internet Information Server
• HTTP ir pieprasījuma/atbildes (request/response)
protokols starp klientiem un serveriem
Web lapas
•
Parasti klienti pieprasa no serveriem web lapas
•
Par web lapu sauc World Wide Web informācijas resursu,
kuram var piekļūt ar pārlūkprogrammas palīdzību
•
Šī informācija parasti ir HTML vai XHTML formātā
•
Katrai web lapai ir URL adrese
•
No vienas web lapas var aiziet uz citām lapām izmantojot
hiperteksta saites
•
Web lapa sastāv no “objektiem”
• teksts, bildes, audio, video
URL - Universal Resource Locators
Optional server port (default = port 80)
www.someSchool.edu:8080/someDept/pic.gif
Server domain name
• URL sastāvdaļas:
•
•
•
servera adrese
(neobligāts) porta numurs
ceļš līdz resursam
Object path name
Klienta/servera saziņas scenārijs
Web klientu/serveru sazināšanas notiek pa
HTTP protokolu
1.
2.
3.
4.
Lietotājs norāda web lapas
URLu pārlūkprogrammā
Pārlūkprogramma sūta HTTP
pieprasījumu serverim
Serveris apstrādā pieprasījumu
un sūta pieprasītu web lapu
atpakaļ klientam
Pārlūkprogramma rāda web
lapas saturu lietotājam
HTTP pārskats
• HTTP ir standarts sazināšanas protokols World
Wide Web informācijas pārsūtīšanai
• HTTP apraksta ziņojumus (request/response) ar
kuriem var apmainīties klienti un serveri
• Protoklu izstrādāja W3C (World Wide Web
Consortium) un IETF (Internet Engineering Task Force)
• HTTP versijas:
•
•
HTTP 1.0 (1996) - RFC 1945
Šodienas versija HTTP 1.1 (1999) – RFC 2068
Savienojumi
•
Parasti HTTP klients inicializē pieprasījumu izveidojot
TCP savienojumu pie servera uz 80 porta (kaut gan
formāli tas nav obligāti)
•
HTTP serveris “klausās” šo portu un gaida pieprasījumus
no klientiem
•
HTTP ir “stateless”
•
serveris neglabā informāciju par iepriekšējiem
pieprasījumiem
•
HTTP 1.0 – viens request/response cikls un savienojums
tiek pārtraukts
•
HTTP 1.1 – pastāvīgu savienojumu atbalsts
HTTP piemērs
•
Lietotājs ievada URL, piemērām,
http://www.someSchool.edu/someDept/index.html
•
Pieprasītais objekts satur HTML tekstu un atsauces uz vēl
10 JPEG bildēm
•
Pārlūkprogramma aizsūta
HTML “GET” pieprasījumu
serverim www.someSchool.edu
•
Serveris atrod un aizsūta atpakaļ
HTML failu
•
Pārlūkprogramma lasa failu un
pēc kārtas sūta vēl 10 pieprasījumus
priekš iekļautām JPEG bildēm
HTTP ziņojumu formāts
• Ir divi HTTP ziņojumu tipi:
• pieprasījums (request)
• atbilde (response)
• Ziņojumi ir ASCII formātā
• human-readable format
• HTTP pieprasījuma ziņojums sastāv no:
1. Pieprasījuma rinda (request line)
2. Hederu rindas (header lines)
3. Tukša rinda
4. (neobligāts) Ziņojuma ķermenis (message body)
HTTP pieprasījuma piemērs
•
Pirmkārt, jāizveido savienojumu ar serveri
telnet www.ietf.org 80
• Tad var sūtīt HTTP pieprasījumu
GET /rfc.html HTTP/1.1
Host: www.ietf.org
<tukša rinda>
request line
header
HTTP atbildes formāts
HTTP atbildes ziņojums sastāv no:
1.
2.
3.
4.
Atbildes statusa rinda (response status line)
Hederu rindas (header lines)
Tukša rinda
Pieprasītais objekts, kļūdas paziņojums utml
HTTP/1.1 200 OK
Date: Wed, 26 Sep 2007 16:26:27 GMT
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Tue, 29 Aug 2006 15:18:10 GMT
ETag: "7c90db-cf8-8ce69080"
Accept-Ranges: bytes
Content-Length: 3320
Connection: close
Content-Type: text/html; charset=UTF-8
<HTML>
. . .
Pilns telnet piemērs
Pieslēgšanas HTTP
servera portam
Telnet output
GET komanda
HTTP atbildes
statusa rinda
HTTP atbildes
hederi
Objekta saturs
Telnet output
> telnet www.cs.unc.edu 80
Trying 152.2.131.240...
Connected to rock.cs.unc.edu.
Escape character is '^]'.
GET /~jeffay/foo.txt HTTP/1.1
HTTP/1.1 200 OK
Date: Mon, 11 Feb 2002 18:25:31 GMT
Server: Apache/1.3.23 (Unix)
Last-Modified: Mon, 11 Feb 2002 18:25:02 GMT
ETag: "190693ce-b1-3c680c7e"
Accept-Ranges: bytes
Content-Length: 177
Connection: close
Content-Type: text/plain
** This test file is stored in the UNIX
** file system at
** /afs/cs.unc.edu/home/jeffay/public_html/foo.txt
Connection closed by foreign host.
HTTP pieprasījuma metodes
• HTTP definē astoņas metodes, kas apzīmē
darbības kuru var veikt ar noradītu resursu
•
•
•
•
•
•
•
•
GET
POST
HEAD
PUT
DELETE
TRACE
OPTIONS
CONNECT
HTTP request line:
<METHOD> <PATH> <VERSION>
GET /index.html HTTP/1.1
POST /login.jsp HTTP/1.1
GET metode
•
GET metode tiek izmantota lai vienkārši saņemt
informāciju no servera noradot resursa URL
•
GET ir pati izplatītāka
metode
•
GET metode tiek izmantota arī lai aizsūtīt serverim
informāciju no nelielas formas
GET /index.html HTTP/1.1
Host: www.someHost.com
<FORM NAME="myform" ACTION="cgi-bin/validate.cgi" METHOD="GET">
First Name: <INPUT TYPE="TEXT" NAME="fname" SIZE="20"><BR>
Last Name: <INPUT TYPE="TEXT" NAME="lname" SIZE="20"> <BR>
<INPUT TYPE="SUBMIT" VALUE="SUBMIT">
</FORM>
www.sitename/cgi-bin/validate.cgi?fname=John&lname=Doe
POST metode
•
Iespēja aizsūtīt datus apstrādei uz servera
•
Piemēri - liela forma, lietotāja paroļu nosūtīšana,
failu augšupielāde
POST /login.jsp HTTP/1.1
•
Datus norāda ziņojuma
ķermenī
Host: www.mysite.com
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type:
application/x-www-form-urlencoded
userid=joe&password=guessme
•
Metodes izpildes rezultātā var tikt izveidots jauns
resurss, vai arī izmainīts jau eksistējošs resurss utml
GET un POST atšķirības
•
Tehniski atšķirība starp GET un POST ir datu kodēšanas
formā
•
GET – dati tiek iekodēti URLā
•
POST – dati paradās ziņojuma ķermenī
•
Galvenā atšķirība – GET ir droša (safe) operācija, kas
nemaina servera stāvokļi (no side effects), bet POST ir
nedroša
•
GET ir jāizmanto vienīgi datu iegūšanai, pieprasījumiem
•
Idempotence – daudzkārtējiem pieprasījumiem tāds pats
efekts kā vienam pieprasījumam
•
GET metodei ir jābūt idempotentai
Citas metodes
HEAD
Asks for the response identical to the one that would
correspond to a GET request, but without the response
body
PUT
Uploads a representation of the specified resource
DELETE
Deletes the specified resource
TRACE
Echoes back the received request, so that a client can see
what intermediate servers are adding or changing in the
request
OPTIONS
Returns the HTTP methods that the server supports. This
can be used to check the functionality of a web server
CONNECT
Converts the request connection to a transparent TCP/IP
tunnel, usually to facilitate SSL-encrypted communication
(HTTPS) through an unencrypted HTTP proxy
HTTP pieprasījuma hederi
Header
Description
Example
Accept
Content-Types that are acceptable
Accept: text/plain
Accept-Charset
Character sets that are acceptable
Accept-Charset: iso-8859-5
Accept-Encoding
Acceptable encodings
Accept-Encoding: gzip
Accept-Language
Acceptable languages for response
Accept-Language: da
Authorization
Authentication credentials for
HTTP authentication
Authorization: Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Connection
What type of connection the useragent would prefer
Connection: close
Date
The data and time that the message
was sent
Date: Tue, 15 Nov 1994
08:12:31 GMT
Host
The domain name of the server (for
virtual hosting)
Host: www.w3.org
User-Agent
The user agent string of the user
agent
User-Agent: Mozilla/5.0
(Linux; X11; UTF-8)
MIME tipi
•
•
MIME = Multipurpose Internet Mail Extension
Failu formāta identifikators, oriģināli bija definēts e-pastam
Tips
Nozīme
application/x-java-archive
JAR fails
application/xhtml+xml
XHTML fails
application/zip
ZIP arhīvs
text/html
HTML fails
text/plain
Vienkārša teksta (TXT) fails
text/xml
XML fails
image/gif
GIF bilde
“Accept” hederis pieprasījumā, “Content-Type” hederis atbildē
"x-" prefikss apakštipam norāda, ka šis faila tips nav oficiāli reģistrēts
HTTP atbildes statusa kodi
• HTTP atbildes pirmā rinda satur protokola
versiju, statusa koda ciparu un paskaidrojošo
teksta frāzi
HTTP/1.1 200 OK
• Klienti var izmantot statusa kodus, lai korekti
attēlot informāciju
• Statusa koda pirmais cipars norāda uz koda klasi
•
•
•
•
•
1XX – Informational
2XX – Success
3XX – Redirection
4XX – Client Error
5XX – Server Error
HTTP statusa kodi
200
OK
Dokuments tiek sekmīgi nosūtīts
302
Moved
Temporarily
Izmanto kopā ar "Location:" hederi, lai nosūtītu
servera puses redirektu
304
Not Modified
Serveris nesūta dokumentu, bet liek klientam
lietot kešoto versiju
401
Unauthorized
Klients nav pareizi uzstādījis "Authorization"
hederi
403
Forbidden
Serveris neļauj skatīties pieprasīto resursu
404
Not Found
Visizplatītākais kods
500
Internal Server Kļūda servera puses aplikācijā
Error
Statusa kodu apstrāde
HTTP atbildes hederi
Accept-Ranges
What partial content range types
this server supports
Cache-Control
Controls how proxies may cache Cache-Control: no-cache
this object
Content-Length
The length of the content in
bytes
Content-Length: 348
Content-Range
Where in a full body message
this partial message belongs
Content-Range: bytes 2101047021/47022
Content-Type
The mime type of this content
Content-Type: text/html;
charset=utf-8
Last-Modified
The last modified date for the
requested object
Last-Modified: Tue, 15 Nov
1994 12:45:26 GMT
Location
Used in redirection
Location:http://www.w3.org/pub
/WWW/People.html
Server
A name for the server
Server: Apache/1.3.27 (Unix)
(Red-Hat/Linux)
Accept-Ranges: bytes
Sesijas stāvokļa uzturēšana
• Bieži Web lietojumprogrammās ir nepieciešams
uzturēt un kontrolēt lietotāja sesijas stāvokli
•
•
•
lietotāja reģistrācija
web lapas uzstādījumi
pirkšanas groza saturs Internet veikalā
• HTTP ir “stateless”, tāpēc lietotāja sesijas
uzturēšanai ir jālieto speciālas metodes
•
•
•
•
cookies
slēptie formas mainīgie
URL pārrakstīšana
servera puses sesijas
Cookies
•
Viens no galvenajiem sesijas uzturēšanas līdzekļiem
•
Cookie ir teksta fails, kuru Web serveris aizsūta klientam, kurš
saglabā to uz lokāla diska un nākamajos pieprasījumos sūta
nemainītu atpakaļ serverim
browser  server
GET /index.html HTTP/1.1
Host: www.w3.org
browser  server
GET /spec.html HTTP/1.1
Host: www.w3.org
Cookie: name=value
Accept: */*
browser  server
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
(content of page)
. . .
Web roboti
• Web robots (web crawler, web spider) –
programma vai automatizēts skripts, kurš
metodiski apstaiga World Wide Web
• Tipiskie uzdevumi:
•
•
•
up-to-date informācijas nodrošināšana meklēšanas
programmās
apstaigātu lapu saglabāšana turpmākai apstrādāšanai
HTML koda validācija, linku pārbaude utml
Web robotu arhitektūra
Robotu pieklājības politika
• Web roboti var ievērojami ietekmēt web lapu
darbspēju
•
•
vairāki pieprasījumi sekundē
lielo failu augšupielāde
• Lai kontrolēt problēmu ir izstrādāts robotu
izslēgšanas protokols (robots.txt protokols)
• Var apskatīties, piemērām,
http://del.icio.us/robots.txt
Apache HTTP Client
•
Web resursu piekļuvei ar HTTP var izmantot Java SE
java.net pakotni
•
Taču bāzes funkcionalitāte nenodrošina pilnu elastīgumu
un funkcionalitāti
•
Jakarta Commons HttpClient komponents piedāvā
efektīvu , modernu un daudz-funkcionālu risinājumu
•
Paņemt HttpClient no
http://jakarta.apache.org/httpcomponents/httpclient-3.x/
•
Pielikt commons-httpclient-3.1.jar pie classpath
•
Izmantot klases no org.apache.commons.httpclient
Izmantošanas piemērs
// izveido jauno klienta instanci
HttpClient client = new HttpClient();
// izveido GET metodi
GetMethod method = new GetMethod(url);
// uzliek parametrus
method.getParams().setParameter(
HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
// izpilda metodi
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("Method failed: " + method.getStatusLine());
}
// saņem atbildes ķermeni, web lapas saturu
byte[] page = method.getResponseBody();
// atbrīvo savienojumu
method.releaseConnection();
Resursi
• HTTP by W3C
http://www.w3.org/Protocols/
• HTTP Session Tracking
http://www.unix.org.ua/orelly/javaent/servlet/ch07_01.htm
• Par Web robotiem
http://en.wikipedia.org/wiki/Web_crawler
• Apache HTTP Client
http://jakarta.apache.org/httpcomponents/httpc
lient-3.x/