9 Temmuz 2020 Perşembe

Http Header Parametreleri

Not:  Konuyla ilgili olarak Http header injection yazına bakmakta fayda var.

Http Header Nedir?
Http Header bir Http istek veya cevabının (Http Request or Response) başında bulunan veridir.
Http paketi

1.Http Header
2.Body
3. Trailer

alanlarından oluşur. Http Header alanları ile HTML'i birbiriyle karıştırmamak gerekir.
Http Header alanları RFC 2616'da tanımlı.


Http Header Hangi Kısımlardan Oluşur
Http Header da kendi içinde bölümlenmiştir. İki ana kısımdan oluşur.

1. Request veya Response isimli alanlar.
2. MIME Type ile ilgili alanlar. Aslında MIME alışkanlıktan dolayı kullanılana bir kelime. MIME yerine Internet Media Type kullanılmalı. MIME Type ile ilgili alanlar hem Request hem de Response ile birlikte kullanılabilir. Örneğin dosya yüklemek için POST veya PUT ile birlikte kullanılır. Dosya indirirken Response ile birlikte gönderilir.

Bu yazıdaki çoğu parametre HTTP Header kısmı ile ilgili.

Request Alanı
Http verb'lerini içerir. Açıklaması şöyle
Method         = "OPTIONS"                ; Section 9.2
                  | "GET"                    ; Section 9.3
                  | "HEAD"                   ; Section 9.4
                  | "POST"                   ; Section 9.5
                  | "PUT"                    ; Section 9.6
                  | "DELETE"                 ; Section 9.7
                  | "TRACE"                  ; Section 9.8
                  | "CONNECT"                ; Section 9.9
                  | extension-method
   extension-method = token
Verb'ler harflere duyarlıdır. Açıklaması şöyle
The Method token indicates the method to be performed on the
resource identified by the Request-URI. The method is case-sensitive.
Http2 protokolü Http/1.x'e göre çok daha karmaşık.

Http Head Verb
Head isteği Get isteği ile aynı. Aradaki tek fark istenilen şeyin içeriği yerine sadece meta-date bilgilerinin gelmesi. How to calculate a file size from URL in java sorusunda benzer bir cevap var.

Http Header Harf Duyarlılığı
Açıklaması şöyle
Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.
Not : Bazı parametreleri Http İstek Parametreleri yazısına taşıdım.
Not : Bazı parametreleri  Http Cevap Parametreleri yazısına taşıdım.

Connection Parametresi
Http 1.0'da bu parametre mevcut değil. Sunucu bağlantıyı istek sonunda otomatik kapatır
Http 1.1'de sunucu bağlantıyı ne kadar açık tutacağını şöyle belirtir.
Connection: keep-alive
Keep-Alive: timeout=300
Hata ve zaman aşımının açıklaması şöyle. Taraflardan herhangisi birisi bağlantıyı istediği an kapatabilir.
6.5. Failures and Timeouts
A client, server, or proxy MAY close the transport connection at any time. [...]
A client sending a message body SHOULD monitor the network connection for an error response while it is transmitting the request. If the client sees a response that indicates the server does not wish to receive the message body and is closing the connection, the client SHOULD immediately cease transmitting the body and close its side of the connection.
Kapatmanın açıklaması şöyle
6.6. Tear-down
A server that sends a "close" connection option MUST initiate a close of the connection [...] after it sends the response containing "close". [...]
Kapatmak için şöyle bir veri alırız. Elle kodlanan basit bir web sunucusunda bu alanı kullanmadan direkt soketi kapatmıştım :)
Connection:close
Content-Type:application/json;charset=UTF-8
Date:Wed, 29 Mar 2017 03:30:51 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Content-Security-Policy Parametresi
Örnek
Şöyle yaparız.
Content-Security-Policy : default-src 'self'; (Allow everything but only from the same origin)
Content-Security-Policy : script-src 'self';  (Only Allow Scripts from the same origin)
Content-Security-Policy : script-src 'self' www.google-analytics.com ajax.googleapis.com; (Allow Google Analytics, Google AJAX CDN and Same Origin)
Content-Security-Policy : default-src https: (Allow any assets to be loaded over https from any origin)
Örnek
Scipt, frame, connection gibi şeyler etkinsizleştirip sadece image ve stylesheet'lerin kendi alanımızdan yüklenmesine izin vermek için şöyle yaparız.
Content-Security-Policy: default-src 'none'; img-src 'self'; style-src 'self';
Content-type Parametresi
Content-type MIME header alanlarından birisi. Gönderilen dosyanın ne tip olduğunu anlamak için ya dosyanın uzantısına bakmak veya magic number kullanmak gerekirdi. Bunun yerine bir standart oluşturularak dosyanın ne tip bir şey olduğu belirlenmiş. Dosya tipleri de belli başlıklar altında toplanmış. Bu başlıklar metin, audio, video gibi şeyler.
1. Metin Tipi
İnsan tarafından okunabilen metin olduğunu belirtir. Metin verisi genellikle character encoding ile beraber kullanılır. Mesela charset=UTF-8 gibi.

text/plain
"text/plain" ile gönderilen verinin metin olduğu belirtilir.

text/html
Örnek
Şöyledir.
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Fri, 08 Apr 2016 02:10:14 GMT
Content-Type: text/html
Content-Length: 286
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip
Örnek
text/html;charset=ISO-8859-1 yapılırsa gönderilen içeriğin html olduğu ve encoding olarak ne kullandığı belirtilir. Metin olarak gönderilen verinin hangi encoding kullandığını belirtmekte fayda var. utf-8 için şöyle yaparız.
'Content-Type: text/html; charset=utf-8'
text/xml
Örnek
Şöyledir
POST /jwsbook/BookQuote HTTP/1.1
Host: www.Monson-Haefel.com
Content-Type: text/xml; charset="utf-8"
Content-Length: 295
SOAPAction=""

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:mh="http://www.Monson-Haefel.com/jwsbook/BookQuote">
   <soap:Body>
      <mh:getBookPrice>
          <isbn>0321146182</isbn>
      </mh:getBookPrice>
   </soap:Body>
</soap:Envelope>

2. Application Tipi
Multipurpose dosyalar için application grubu kullanılır.

Json Verisi
"application/json; charset=UTF-8" şeklinde veriliyor.

PDF Verisi
"application/pdf" şeklinde veriliyor. Tarayıcı bu linki kendi PDF göstericisi ile açar veya kendisini pdf MIME tipi için sisteme kaydetmiş uygulama ile açar.

Binary Veri
"application/octet-stream" verinin binary olduğunu belirtir. Tarayıcı bu linki diske kaydeder.
Örnek
Şöyle yaparız

curl -ILk http://clhs.lisp.se/Body/m_w_open.htm
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Accept-Ranges: bytes
Content-Length: 8896
Date: Sat, 21 Sep 2019 18:41:10 GMT
Server: lighttpd/1.4.45

Form Verisi
Http Post ve multipart/form-data yazısına taşıdım.


Excel
"application/vnd.ms-excel" şeklinde gönderilir.
Örnek
Sunucundan excel dosyası göndermek ve harici bir uygulama ile açmak için şöyle yaparız.
String fileName = "MyFile.xls";

response.setContentType("application/vnd.ms-excel"); //Tell browser to expect an excel
response.setHeader("Content-Disposition", "attachment; filename="+fileName);

HSSFWorkbook workbook = createExcel();
workbook.write(response.getOutputStream());
SOAP
Şöyle yaparız.
'Content-Type: application/soap+xml; charset=UTF-8'
Şöyle yaparız.
Content-Type: application/soap+xml; charset=utf-8
3. Unregistered Tip
token isteme
Örnek
Şöyle yaparız.
params.put("Content-Type","application/x-www-form-urlencoded");

Content-Encoding Parametresi
Http Cevap Parametreleri yazısına taşıdımf

Content-Disposition Parametresi
Bu parametre MIME header alanlarından birisi. inline veya attachment değerini alabilir. Eğer belirtilmezse inline varsayılır.

inline
Bu parametre inline ise browser içinde açılır.
Örnek
Şöyle yaparız. Bu bir png dosyası eğer gösterebiliyorsan göster, gösteremiyorsan belirtilen dosya ismi ile kaydet anlamına gelir.
Content-Type: image/png
Content-Disposition: inline; filename="picture.png"
Örnek
form-data upload için kullanılır. Şöyle yaparız.
"Content-Disposition: form-data; name="id"\r\n\r\nTTR";
Örnek
Şöyle yaparız.
"Content-Disposition: form-data; name="file"; 
filename="C:\test.jpg"\r\nContent-Type: image/jpeg\r\n\r\n"
attachment
Bu parametre attachment ise gönderilen dosya browser içindeki plug-in tarafından değil de harici bir uygulama tarafından açılır. Aynı zamanda dosyaya isim verebiliriz. Verinin tipini bilmiyorsak açıklaması şöyle
If this header [Content-Disposition: attachment] is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.
Örnek
Şöyle yaparız.
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="picture.png"
Örnek
Eğer verinin tipini biliyorsak şöyle yaparız. Bu bir png dosyası ve kaydet anlamına gelir.
Content-Type: image/png
Content-Disposition: attachment; filename="picture.png"
Örnek
pdf için şöyle yaparız.
Content-Disposition: attachment; filename="Schubert-Sonata-21-B-flat.pdf"
Content-Type: application/pdf
Cookie Parametresi
Cookie'ler '; ' karakteri ile ayrılır. Şöyle yaparız.
Cookie: XSRF-TOKEN=...; laravel_session=...
Last-Modified Parametresi
Bu parametre bir sayfanın en son ne zaman değiştiğini bildirir.

Proxy-Connection Parametresi
Eğer client, sunucunun bağlantıyı kapatmasını istemiyorsa, yani persistent connection istiyorsa
Connection : keep-alive gönderir.

Eğer arada proxy varsa, proxy yazılımının bu isteğe uygun davranması gerekir. Ancak eski proxy yazılımları bu isteğe uygun davranmıyordu. Bu yüzden geçici çözüm olarak Proxy-Connection parametresi icat edildi. Açıklaması şöyle.
One attempted solution was the introduction of a Proxy-Connection header field, targeted specifically at proxies.
Bir müddet sonra Proxy yazılımların akıllanarak bu alandadan kurtulunacağı düşünülüyordu. Dolayısıyla normalde bu alanı göndermemek lazım. Açıklaması şöyle.
As a result, clients are encouraged not to send the Proxy-Connection header field in any requests.
Ancak bu iş beklendiği gibi olmadı.Açıklaması şöyle. Dolayısıla ortada halen bir sürü eski proxy yazılımı var.
Back in 2016 we (in the curl project) actually removed the Proxy-Connection: Keep-Alive header from curl requests done to proxies for exactly that reason: it isn't necessary since the protocol implies keep-alive and the spec says so!

Then (after that change) we immediately got a stream of bug reports from people whose proxy connections broke down completely and persistent connections simply failed to work... and once we reverted that change everything went back to working again.
Örnek
Şöyle yaparız.
"Proxy-Connection: keep-alive";
"Connection: keep-alive";
Transfer-Encoding Parametresi
Şu değerlerden birisini alır
chunked,compress,deflate,gzip,identity
chunk seçeneği
HTTP2 ile kullanımdna kalkmıştır. Veriyi chunk halinde gönderebilmeye yarıyor. Açıklaması şöyle.
When the server needs to send large amount of data, chunked encoding is used by the server because it did not exactly know how big (length) the data is going to be. In HTTP terms, when server sends response Content-Length header is omitted by the server. Instead server writes the length of current chunk in hexadecimal format followed by \r\n and then chunk, followed by \r\n (Content begins with chunk size in hex followed by chunk)

This feature can be used for progressive rendering; however the server needs to flush the data as much as possible so that client can render content progressively (in case of html,css etc)

This feature is often used when server pushes data to the client in large amounts - usually in large size (mega/giga)

Örnek
Video streaming için kullanılabilir.

Örnek
Text için şöyle yaparız.
HTTP/1.1 200 OK\r\n
Transfer-Encoding: chunked\r\n
Content-Type: text/plain\r\n
\r\n
1e\r\n
Uh-oh, this will never stop.\n
1e\r\n
Uh-oh, this will never stop.\n
X-XSS-Protection Parametresi
Açıklaması şöyle
Setting this in HTTP response header will turn on the browsers built in XSS protection
Şöyle yaparız.
X-XSS-Protection: 1; mode=block

4 yorum: