亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄 搜索
Guides Access control CORS Authentication Browser detection using the user agent Caching Caching FAQ Compression Conditional requests Connection management in HTTP 1.x Content negotiation Content negotiation: List of default Accept values Cookies CSP Messages Overview Protocol upgrade mechanism Proxy servers and tunneling Proxy servers and tunneling: Proxy Auto-Configuration (PAC) file Public Key Pinning Range requests Redirections Resources and specifications Resources and URIs Response codes Server-Side Access Control Session Guides: Basics Basics of HTTP Choosing between www and non-www URLs Data URIs Evolution of HTTP Identifying resources on the Web MIME Types MIME types: Complete list of MIME types CSP Content-Security-Policy Content-Security-Policy-Report-Only CSP: base-uri CSP: block-all-mixed-content CSP: child-src CSP: connect-src CSP: default-src CSP: font-src CSP: form-action CSP: frame-ancestors CSP: frame-src CSP: img-src CSP: manifest-src CSP: media-src CSP: object-src CSP: plugin-types CSP: referrer CSP: report-uri CSP: require-sri-for CSP: sandbox CSP: script-src CSP: style-src CSP: upgrade-insecure-requests CSP: worker-src Headers Accept Accept-Charset Accept-Encoding Accept-Language Accept-Ranges Access-Control-Allow-Credentials Access-Control-Allow-Headers Access-Control-Allow-Methods Access-Control-Allow-Origin Access-Control-Expose-Headers Access-Control-Max-Age Access-Control-Request-Headers Access-Control-Request-Method Age Allow Authorization Cache-Control Connection Content-Disposition Content-Encoding Content-Language Content-Length Content-Location Content-Range Content-Type Cookie Cookie2 Date DNT ETag Expect Expires Forwarded From Headers Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since Keep-Alive Large-Allocation Last-Modified Location Origin Pragma Proxy-Authenticate Proxy-Authorization Public-Key-Pins Public-Key-Pins-Report-Only Range Referer Referrer-Policy Retry-After Server Set-Cookie Set-Cookie2 SourceMap Strict-Transport-Security TE Tk Trailer Transfer-Encoding Upgrade-Insecure-Requests User-Agent User-Agent: Firefox Vary Via Warning WWW-Authenticate X-Content-Type-Options X-DNS-Prefetch-Control X-Forwarded-For X-Forwarded-Host X-Forwarded-Proto X-Frame-Options X-XSS-Protection Methods CONNECT DELETE GET HEAD Methods OPTIONS PATCH POST PUT Status 100 Continue 101 Switching Protocols 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 307 Temporary Redirect 308 Permanent Redirect 400 Bad Request 401 Unauthorized 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Payload Too Large 414 URI Too Long 415 Unsupported Media Type 416 Range Not Satisfiable 417 Expectation Failed 426 Upgrade Required 428 Precondition Required 429 Too Many Requests 431 Request Header Fields Too Large 451 Unavailable For Legal Reasons 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 511 Network Authentication Required Status
文字

瀏覽器發(fā)送特定的HTTP標(biāo)頭,用于從內(nèi)部XMLHttpRequest或獲取API發(fā)起的跨站點請求。它還希望看到具有跨站點響應(yīng)的特定HTTP標(biāo)頭。HTTP訪問控制(CORS)文章中提供了這些頭文件的概述,包括啟動請求和處理來自服務(wù)器的響應(yīng)的示例JavaScript代碼以及對每個頭文件的討論,應(yīng)該將其作為配套文章這個。本文介紹處理訪問控制請求和制定訪問控制響應(yīng)在PHP中。本文的目標(biāo)受眾是服務(wù)器程序員或管理員。盡管這里顯示的代碼示例是用PHP編寫的,但類似的概念適用于ASP.net,Perl,Python,Java等。通常,這些概念可以應(yīng)用于處理HTTP請求并動態(tài)地制定HTTP響應(yīng)的任何服務(wù)器端編程環(huán)境。

討論HTTP標(biāo)頭

這篇文章涵蓋了客戶端和服務(wù)器使用的HTTP標(biāo)頭,并且應(yīng)該被視為讀取前提條件。

工作代碼示例

后續(xù)章節(jié)中的PHP片段(以及對服務(wù)器的JavaScript調(diào)用)取自此處發(fā)布的工作代碼示例。這些將在實現(xiàn)跨站點的瀏覽器中工作XMLHttpRequest。

簡單的跨網(wǎng)站請求

簡單訪問控制請求在以下情況下啟動:

  • 一個HTTP/1.1 GET或者一個POST被用作請求方法。在POST的情況下,Content-Type請求體的是一種application/x-www-form-urlencodedmultipart/form-datatext/plain.

  • 沒有自定義標(biāo)頭與HTTP請求一起發(fā)送(例如X-Modified等)

在這種情況下,可以根據(jù)一些考慮將回復(fù)發(fā)送回去。

  • 如果有問題的資源被廣泛訪問(就像GET訪問的任何HTTP資源一樣),那么發(fā)送回頭Access-Control-Allow-Origin: *就足夠了,除非資源需要諸如Cookie和HTTP認(rèn)證信息之類的憑證。

  • 如果資源應(yīng)該基于請求者域保持限制,或者如果資源需要使用憑據(jù)訪問(或設(shè)置憑證),則Origin可能需要按請求頭進(jìn)行過濾,或者至少回應(yīng)請求者Origin(例如Access-Control-Allow-Origin: http://arunranga.com)。此外,Access-Control-Allow-Credentials: true標(biāo)題將不得不發(fā)送。這將在后面的章節(jié)中討論。

簡單訪問控制請求部分顯示客戶端和服務(wù)器之間的頭部交換。這是處理簡單請求的PHP代碼段:

<?php// We'll be granting access to only the arunranga.com domain // which we think is safe to access this resource as application/xmlif($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") {    header('Access-Control-Allow-Origin: http://arunranga.com');    header('Content-type: application/xml');    readfile('arunerDotNetResource.xml');} else {    
  header('Content-Type: text/html');
  echo "<html>";
  echo "<head>";
  echo "   <title>Another Resource</title>";
  echo "</head>";
  echo "<body>",       "<p>This resource behaves two-fold:";
  echo "<ul>",         "<li>If accessed from <code>http://arunranga.com</code> it returns an XML document</li>";
  echo   "<li>If accessed from any other origin including from simply typing in the URL into the browser's address bar,";
  echo   "you get this HTML document</li>", 
       "</ul>",     "</body>",   "</html>";}?>

上面的內(nèi)容檢查Origin瀏覽器發(fā)送的頭文件(通過$ _SERVER'HTTP_ORIGIN'獲得)是否匹配' http://arunranga.com '。如果是,它會返回Access-Control-Allow-Origin: http://arunranga.com。

預(yù)先請求的請求

預(yù)沖的訪問控制請求在下列情況下發(fā)生:

  • 以外的方法,GETPOST使用,或如果POST使用具有Content-Type 比其它的一個application/x-www-form-urlencoded,multipart/form-datatext/plain。舉例來說,如果Content-Type所述的POST體是application/xml,請求預(yù)檢。

  • 自定義標(biāo)題(例如X-PINGARUNER)與請求一起發(fā)送。

Preflighted訪問控制請求部分顯示客戶端和服務(wù)器之間的頭部交換。響應(yīng)預(yù)檢請求的服務(wù)器資源需要能夠做出以下決定:

  • 基于Origin(如果有的話)過濾。

  • 響應(yīng)于OPTIONS請求(這是預(yù)檢請求),包括與發(fā)送必要的值Access-Control-Allow-Methods,Access-Control-Allow-Headers(如果需要,為了任何附加頭的應(yīng)用程序的工作),并且,如果證書是必要的這一資源,Access-Control-Allow-Credentials。

  • 對實際請求的回應(yīng),包括處理POST數(shù)據(jù)等。

以下是處理預(yù)發(fā)光請求的PHP示例:

<?php 

if($_SERVER['REQUEST_METHOD'] == "GET") {  header('Content-Type: text/plain');
  echo "This HTTP resource is designed to handle POSTed XML input";
  echo "from arunranga.com and not be retrieved with GET"; } elseif($_SERVER['REQUEST_METHOD'] == "OPTIONS") {  // Tell the Client we support invocations from arunranga.com and   // that this preflight holds good for only 20 days  if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") {    header('Access-Control-Allow-Origin: http://arunranga.com');    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');    header('Access-Control-Allow-Headers: X-PINGARUNER');    header('Access-Control-Max-Age: 1728000');    header("Content-Length: 0");    header("Content-Type: text/plain");    //exit(0);  } else {    header("HTTP/1.1 403 Access Forbidden");    header("Content-Type: text/plain");
    echo "You cannot repeat this request";  }} elseif($_SERVER['REQUEST_METHOD'] == "POST") {  // Handle POST by first getting the XML POST blob,   // and then doing something to it, and then sending results to the client 
  if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") {
    $postData = file_get_contents('php://input');
    $document = simplexml_load_string($postData);    
    // do something with POST data

    $ping = $_SERVER['HTTP_X_PINGARUNER'];         
    header('Access-Control-Allow-Origin: http://arunranga.com');    header('Content-Type: text/plain');
    echo // some string response after processing  } else {    die("POSTing Only Allowed from arunranga.com");  }} else {    die("No Other Methods Allowed");}?>

請注意,為了響應(yīng)OPTIONS預(yù)檢和POST數(shù)據(jù),將返回相應(yīng)的標(biāo)題。因此一個資源處理預(yù)檢和實際請求。在對OPTIONS請求的響應(yīng)中,服務(wù)器通知客戶端實際的請求確實可以用該POST方法進(jìn)行,并且諸如頭部字段X-PINGARUNER可以與實際請求一起發(fā)送。這個例子可以在這里看到。

認(rèn)證請求

憑證訪問控制請求 - 即伴隨有Cookie或HTTP身份驗證信息的請求(并且期望Cookie與響應(yīng)一起發(fā)送) - 可以是簡單或預(yù)檢,具體取決于所使用的請求方法。

在簡單請求方案中,請求將以Cookie發(fā)送(例如,如果withCredentials標(biāo)志設(shè)置為開啟XMLHttpRequest)。如果服務(wù)器響應(yīng)Access-Control-Allow-Credentials: true附加的憑證響應(yīng),則響應(yīng)被客戶端接受并暴露給Web內(nèi)容。在預(yù)檢請求,服務(wù)器可以響應(yīng)Access-Control-Allow-Credentials: trueOPTIONS請求。

以下是一些處理憑證請求的PHP:

<?phpif($_SERVER['REQUEST_METHOD'] == "GET") {  header('Access-Control-Allow-Origin: http://arunranga.com');  header('Access-Control-Allow-Credentials: true');  header('Cache-Control: no-cache');  header('Pragma: no-cache');  header('Content-Type: text/plain');  // First See if There Is a Cookie     if (!isset($_COOKIE["pageAccess"])) {    setcookie("pageAccess", 1, time()+2592000);
    echo 'I do not know you or anyone like you so I am going to';
    echo 'mark you with a Cookie :-)';    
  } else {
    $accesses = $_COOKIE['pageAccess'];    setcookie('pageAccess', ++$accesses, time()+2592000);
    echo 'Hello -- I know you or something a lot like you!';
    echo 'You have been to ', $_SERVER['SERVER_NAME'], ';
    echo 'at least ', $accesses-1, ' time(s) before!';  }  } elseif($_SERVER['REQUEST_METHOD'] == "OPTIONS") {  // Tell the Client this preflight holds good for only 20 days  if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") {    header('Access-Control-Allow-Origin: http://arunranga.com');    header('Access-Control-Allow-Methods: GET, OPTIONS');    header('Access-Control-Allow-Credentials: true');    header('Access-Control-Max-Age: 1728000');    header("Content-Length: 0");    header("Content-Type: text/plain");  } else {    header("HTTP/1.1 403 Access Forbidden");    header("Content-Type: text/plain");
    echo "You cannot repeat this request";  }} else {  die("This HTTP Resource can ONLY be accessed with GET or OPTIONS");}?>

請注意,對于有證書請求的情況,Access-Control-Allow-Origin:標(biāo)頭不得有通配符值“*”。它必須提到一個有效的原始域。上面的例子可以看到在這里運行。

Apache示例

限制對某些URI的訪問

一個有用的技巧是使用Apache重寫,環(huán)境變量和標(biāo)頭來應(yīng)用于Access-Control-Allow-*某些URI。例如,這對于將GET /api(.*).json請求的跨請求限制為沒有憑證的請求很有用:

RewriteRule ^/api(.*)\.json$ /api$1.json [CORS=True]Header set Access-Control-Allow-Origin "*" env=CORS
Header set Access-Control-Allow-Methods "GET" env=CORS
Header set Access-Control-Allow-Credentials "false" env=CORS

另請參閱

  • Examples of Access Control in Action

  • HTTP Access Control covering the HTTP headers

  • XMLHttpRequest

  • Fetch API

上一篇: 下一篇: