HTTPåºæ¬è®¤è¯
å¨HTTPä¸ï¼åºæ¬è®¤è¯æ¯ä¸ç§ç¨æ¥å 许Webæµè§å¨ï¼æå ¶ä»å®¢æ·ç«¯ç¨åºå¨è¯·æ±æ¶æä¾ä»¥ç¨æ·ååå£ä»¤å½¢å¼çåè¯ã
å¨åéä¹åï¼ç¨æ·å追å ä¸ä¸ªåå·ç¶å串æ¥ä¸å£ä»¤ãå¾åºçç»æå符串åç¨Base64ç®æ³ç¼ç ãä¾å¦ï¼ç¨æ·åæ¯Aladdinï¼å£ä»¤æ¯open sesameï¼æ¼æ¥åçç»ææ¯Aladdin:open sesameï¼ç¶ååç¨Base64ç¼ç ï¼å¾å°QWxhZGRpbjpvcGVuIHNlc2FtZQ==ãBase64ç¼ç çå符串åéåºå»ï¼å¹¶ç±æ¥æ¶è 解ç ï¼å¾å°ä¸ä¸ªç±åå·åéçç¨æ·ååå£ä»¤çå符串ã
è½ç¶å¯¹ç¨æ·ååå£ä»¤ç¨Base64ç®æ³å å¯é常使å¾èç¼é¾ä»¥è¯å«ï¼ä½å¯ä»¥å¾å®¹æå°è¢«è§£ç ï¼å°±å容æç¼ç ä¸æ ·ãç¼ç è¿ä¸æ¥éª¤çç®çä¸æ¯å®å ¨ï¼èæ¯å°ç¨æ·ååå£ä»¤ä¸çéHTTPå ¼å®¹çå符å为HTTPå ¼å®¹çå符ã
æåï¼åºæ¬è®¤è¯æ¯å®ä¹å¨HTTP1.0è§èï¼RFC 1945ï¼ä¸ï¼åç»çæå ³å®å ¨çä¿¡æ¯å¯ä»¥å¨HTTP1.1è§èï¼RFC 2616 ï¼åHTTP认è¯è§èï¼RFC 2617ï¼æ¾å°ã
ç®å½[éè] |
[ç¼è¾]ä¼ç¹
åºæ¬è®¤è¯çä¸ä¸ªä¼ç¹æ¯æææµè¡çç½é¡µæµè§å¨é½æ¯æåºæ¬è®¤è¯ã[1] åºæ¬è®¤è¯å¾å°å¨å¯å ¬å¼è®¿é®çäºèç½ç½ç«ä¸ä½¿ç¨ï¼ææ¶åå¨å°çç§æç³»ç»ä¸ä½¿ç¨ãåæ¥çæºå¶ï¼HTTPæè¦è®¤è¯æ¯ä¸ºæå°åºæ¬è®¤è¯èå¼åçï¼å 许**以ç¸å¯¹å®å ¨çæ¹å¼å¨ä¸å®å ¨çééä¸ä¼ è¾ã
ç¨åºååç³»ç»ç®¡çåææ¶ä¼å¨å¯ä¿¡ç½ç»ç¯å¢ä¸ä½¿ç¨åºæ¬è®¤è¯ï¼ä½¿ç¨Telnetæå ¶ä»ææç½ç»åè®®å·¥å ·æå¨å°æµè¯Webæå¡å¨ãè¿æ¯ä¸ä¸ªéº»ç¦çè¿ç¨ï¼ä½æ¯ç½ç»ä¸ä¼ è¾çå 容æ¯äººå¯è¯»çï¼ä»¥ä¾¿è¿è¡è¯æã
[ç¼è¾]缺ç¹
è½ç¶åºæ¬è®¤è¯é常容æå®ç°ï¼ä½è¯¥æ¹æ¡å»ºç«å¨ä»¥ä¸çå设çåºç¡ä¸ï¼å³ï¼å®¢æ·ç«¯åæå¡å¨ä¸»æºä¹é´çè¿æ¥æ¯å®å ¨å¯ä¿¡çãç¹å«æ¯ï¼å¦æ没æ使ç¨SSL/TLSè¿æ ·çä¼ è¾å±å®å ¨çåè®®ï¼é£ä¹ä»¥ææä¼ è¾ç**åå£ä»¤å¾å®¹æ被æ¦æªã该æ¹æ¡åæ ·æ²¡æ对æå¡å¨è¿åçä¿¡æ¯æä¾ä¿æ¤ã
ç°åçæµè§å¨ä¿å认è¯ä¿¡æ¯ç´å°æ ç¾é¡µææµè§å¨è¢«å ³éï¼æè ç¨æ·æ¸ é¤åå²è®°å½ã[2] HTTP没æ为æå¡å¨æä¾ä¸ç§æ¹æ³æ示客æ·ç«¯ä¸¢å¼è¿è¿äºè¢«ç¼åç**ãè¿æå³çæå¡å¨ç«¯å¨ä¸é ééç覽å¨çæ æ³ä¸ï¼å ¶å¯¦ä¸¦æ²¡æä¸ç§ææçæ¹æ³æ¥ç»åºã
[ç¼è¾]ä¾å
è¿ä¸ä¸ªå ¸åçHTTP客æ·ç«¯åHTTPæå¡å¨ç对è¯ï¼æå¡å¨å®è£ å¨åä¸å°è®¡ç®æºä¸ï¼localhostï¼ï¼å å«ä»¥ä¸æ¥éª¤ï¼
- 客æ·ç«¯è¯·æ±ä¸ä¸ªéè¦èº«ä»½è®¤è¯ç页é¢ï¼ä½æ¯æ²¡ææä¾ç¨æ·ååå£ä»¤ãè¿é常æ¯ç¨æ·å¨å°åæ è¾å ¥ä¸ä¸ªURLï¼ææ¯æå¼ä¸ä¸ªæå该页é¢çé¾æ¥ã
- æå¡ç«¯ååºä¸ä¸ª401åºçç ï¼å¹¶æä¾ä¸ä¸ªè®¤è¯åã
- æ¥å°åºçåï¼å®¢æ·ç«¯æ¾ç¤ºè¯¥è®¤è¯åï¼é常æ¯æ访é®ç计ç®æºæç³»ç»çæè¿°ï¼ç»ç¨æ·å¹¶æ示è¾å ¥ç¨æ·ååå£ä»¤ãæ¤æ¶ç¨æ·å¯ä»¥ç¡®å®åæ¶ã
- ç¨æ·è¾å ¥äºç¨æ·ååå£ä»¤åï¼å®¢æ·ç«¯å¨åå ç请æ±ä¸æ·»å äºè®¤è¯æ¶æ¯å¤´ï¼å¼æ¯base64encode(username+":"+password)ï¼ï¼ç¶åéæ°åéã
- å¨æ¬ä¾ä¸ï¼æå¡å¨æ¥åäºè®¤è¯å¹¶è¿åäºé¡µé¢ãå¦æç¨æ·åéæ³æå£ä»¤ä¸å¯¹ï¼æå¡å¨å¯è½è¿å401åºçç ï¼å®¢æ·ç«¯å¯ä»¥å次æ示ç¨æ·è¾å ¥å£ä»¤ã
注æ: 客æ·ç«¯å¯è½ä¸éè¦ç¨æ·äº¤äºï¼å¨ç¬¬ä¸æ¬¡è¯·æ±ä¸å°±åé认è¯æ¶æ¯å¤´ã
客æ·ç«¯è¯·æ±ï¼æ²¡æ认è¯ä¿¡æ¯ï¼:
GET /private/index.html HTTP/1.0 Host: localhost
(è·éä¸ä¸ªæ¢è¡,以å车ï¼CRï¼å æ¢è¡ï¼LFï¼çå½¢å¼)
æå¡ç«¯åºç:
HTTP/1.0 401 Authorization Required
Server: HTTPd/1.0
Date: Sat, 27 Nov 2004 10:18:15 GMT
WWW-Authenticate: Basic realm="Secure Area"
Content-Type: text/html
Content-Length: 311
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <HTML> <HEAD> <TITLE>Error</TITLE> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"> </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>
客æ·ç«¯ç请æ±ï¼ç¨æ·åâ"Aladdinâï¼å£ä»¤, password âopen sesameâï¼:
GET /private/index.html HTTP/1.0 Host: localhost
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
ï¼è·éä¸ä¸ªç©ºè¡ï¼å¦ä¸æè¿°ï¼
Authorizationæ¶æ¯å¤´çç¨æ·ååå£ä»¤çå¼å¯ä»¥å®¹æå°ç¼ç å解ç ï¼
- ç¨Perl:
use MIME::Base64; print encode_base64('Aladdin:open sesame'), "\n"; print decode_base64('QWxhZGRpbjpvcGVuIHNlc2FtZQ=='), "\n";
- ç¨PHP:
echo base64_encode("Aladdin:open sesame")."\n"; echo base64_decode("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")."\n";
- ç¨Python:
print "Aladdin:open sesame".encode("base64") print "QWxhZGRpbjpvcGVuIHNlc2FtZQ==".decode("base64")
- ç¨Ruby:
require 'base64' puts Base64::encode64("Aladdin:open sesame") puts Base64::decode64("QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
- ç¨Bash
$ echo -n Aladdin:open sesame | base64QWxhZGRpbjpvcGVuIHNlc2FtZQ==
$ echo -n QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 -d Aladdin:open sesame
- ç¨XQuery (eXist-db):
let $encode := util:base64-encode('Aladdin:open sesame')
let $decode := util:base64-decode('QWxhZGRpbjpvcGVuIHNlc2FtZQ==')
- ç¨OpenSSL:
$ printf "Aladdin:open sesame" | openssl enc -base64 QWxhZGRpbjpvcGVuIHNlc2FtZQ==
$ echo "QWxhZGRpbjpvcGVuIHNlc2FtZQ==" | openssl enc -base64 -d Aladdin:open sesame
$
- æå¡ç«¯çåºç:
HTTP/1.0 200 OK
Server: HTTPd/1.0
Date: Sat, 27 Nov 2004 10:19:07 GMT
Content-Type: text/html
Content-Length: 10476
ï¼è·éä¸ä¸ªç©ºè¡ï¼éåæ¯åéå¶é¡µçHTMLææ¬ï¼ã
转载äº:https://my.oschina.net/taisha/blog/77405
下一篇: 多线程共享数据