Skip to content
This repository
Browse code

Manage all 'special' headers of #headers{} and #outh{} records

Some headers, like 'Content-Encoding' or 'Transfer-Encoding' were
not correctly handled. These changes can be useful for everyone who
writes yaws scripts or appmods. In particular, the next version of
the reverse proxy depends on it.
  • Loading branch information...
commit e61b0e36de05f6fed631fbfda95531354ad63b8b 1 parent 54acbb6
Christopher Faulet authored February 21, 2012
52  man/yaws_api.5
@@ -652,10 +652,15 @@ The following list of headers are given special treatment.
652 652
 
653 653
 \fI{connection, What}\fR
654 654
 
655  
-This sets the connection header. If \fIWhat\fR is the special value
  655
+This sets the Connection: header. If \fIWhat\fR is the special value
656 656
 \fI"close"\fR, the connection will be closed once the yaws page is delivered
657 657
 to the client.
658 658
 
  659
+\fI{server, What}\fR
  660
+
  661
+Sets the Server: header. By setting this header, the server's signature will be
  662
+dynamically overloaded.
  663
+
659 664
 \fI{location, Url}\fR
660 665
 
661 666
 Sets the Location: header. This header is typically combined with
@@ -665,14 +670,43 @@ the \fI{status, 302}\fR return value.
665 670
 
666 671
 Sets the Cache-Control: header.
667 672
 
  673
+\fI{expires, What}\fR
  674
+
  675
+Sets the Expires: header.
  676
+
  677
+\fI{date, What}\fR
  678
+
  679
+Sets the Date: header.
  680
+
  681
+\fI{allow, What}\fR
  682
+
  683
+Sets the Allow: header.
  684
+
  685
+\fI{last_modified, What}\fR
  686
+
  687
+Sets the Last-Modified: header.
  688
+
  689
+\fI{etag, What}\fR
  690
+
  691
+Sets the Etag: header.
  692
+
668 693
 \fI{set_cookie, Cookie}\fR
669 694
 
670  
-Prepends a a Set-Cookie: header to the list of previously
  695
+Prepends a Set-Cookie: header to the list of previously
671 696
 set Set-Cookie: headers.
672 697
 
  698
+\fI{content_range, What}\fR
  699
+
  700
+Sets the Content-Range: header.
  701
+
673 702
 \fI{content_type, MimeType}\fR
674 703
 
675  
-Sets the Content-Type header.
  704
+Sets the Content-Type: header.
  705
+
  706
+\fI{content_encoding, What}\fR
  707
+
  708
+Sets the Content-Encoding: header. If this header is defined, no deflate is
  709
+performed by Yaws. So you can compress data by yourself.
676 710
 
677 711
 \fI{content_length, Len}\fR
678 712
 
@@ -682,13 +716,19 @@ some reason want to force a Content-Length: header (and we actually do
682 716
 know the length of the content, we can force yaws to not ship the
683 717
 page chunked.
684 718
 
  719
+\fI{transfer_encoding, What}\fR
685 720
 
686  
-All other headers must be added using the normal HTTP syntax.
687  
-Example:
  721
+Sets the Transfer-Encoding: header.
  722
+
  723
+\fI{www_authenticate, What}\fR
  724
+
  725
+Sets the WWW-Authenticate: header.
688 726
 
689  
-{header, "My-X-Header: gadong"}
690 727
 
  728
+All other headers must be added using the normal HTTP syntax.
  729
+Example:
691 730
 
  731
+\fI{header, {"My-X-Header", "gadong"}}\fR  of \fI{header, "My-X-Header: gadong"}\fR
692 732
 
693 733
 
694 734
 .TP
84  src/yaws.erl
@@ -1623,6 +1623,11 @@ accumulate_header({connection, What}) ->
1623 1623
 accumulate_header({"Connection", What}) ->
1624 1624
     accumulate_header({connection, What});
1625 1625
 
  1626
+accumulate_header({server, What}) ->
  1627
+    put(outh, (get(outh))#outh{server = ["Server: " , What, "\r\n"]});
  1628
+accumulate_header({"Server", What}) ->
  1629
+    accumulate_header({server, What});
  1630
+
1626 1631
 accumulate_header({location, What}) ->
1627 1632
     put(outh, (get(outh))#outh{location = ["Location: " , What, "\r\n"]});
1628 1633
 accumulate_header({"Location", What}) ->
@@ -1634,6 +1639,32 @@ accumulate_header({cache_control, What}) ->
1634 1639
 accumulate_header({"Cache-Control", What}) ->
1635 1640
     accumulate_header({cache_control, What});
1636 1641
 
  1642
+accumulate_header({expires, What}) ->
  1643
+    put(outh, (get(outh))#outh{expires = ["Expires: " , What, "\r\n"]});
  1644
+accumulate_header({"Expires", What}) ->
  1645
+    accumulate_header({expires, What});
  1646
+
  1647
+accumulate_header({date, What}) ->
  1648
+    put(outh, (get(outh))#outh{date = ["Date: " , What, "\r\n"]});
  1649
+accumulate_header({"Date", What}) ->
  1650
+    accumulate_header({date, What});
  1651
+
  1652
+accumulate_header({allow, What}) ->
  1653
+    put(outh, (get(outh))#outh{date = ["Allow: " , What, "\r\n"]});
  1654
+accumulate_header({"Allow", What}) ->
  1655
+    accumulate_header({allow, What});
  1656
+
  1657
+accumulate_header({last_modified, What}) ->
  1658
+    put(outh, (get(outh))#outh{last_modified =
  1659
+                                   ["Last-Modified: " , What, "\r\n"]});
  1660
+accumulate_header({"Last-Modified", What}) ->
  1661
+    accumulate_header({last_modified, What});
  1662
+
  1663
+accumulate_header({etag, What}) ->
  1664
+    put(outh, (get(outh))#outh{etag = ["Etag: " , What, "\r\n"]});
  1665
+accumulate_header({"Etag", What}) ->
  1666
+    accumulate_header({etag, What});
  1667
+
1637 1668
 accumulate_header({set_cookie, What}) ->
1638 1669
     O = get(outh),
1639 1670
     Old = case O#outh.set_cookie of
@@ -1644,6 +1675,12 @@ accumulate_header({set_cookie, What}) ->
1644 1675
 accumulate_header({"Set-Cookie", What}) ->
1645 1676
     accumulate_header({set_cookie, What});
1646 1677
 
  1678
+accumulate_header({content_range, What}) ->
  1679
+    put(outh, (get(outh))#outh{content_range =
  1680
+                                   ["Content-Range: " , What, "\r\n"]});
  1681
+accumulate_header({"Content-Range", What}) ->
  1682
+    accumulate_header({content_range, What});
  1683
+
1647 1684
 accumulate_header({content_type, What}) ->
1648 1685
     put(outh, (get(outh))#outh{content_type = ["Content-Type: " ,
1649 1686
                                                What, "\r\n"]});
@@ -1651,7 +1688,8 @@ accumulate_header({"Content-Type", What}) ->
1651 1688
     accumulate_header({content_type, What});
1652 1689
 
1653 1690
 accumulate_header({content_encoding, What}) ->
1654  
-    put(outh, (get(outh))#outh{content_encoding =
  1691
+    put(outh, (get(outh))#outh{encoding = deflate,
  1692
+                               content_encoding =
1655 1693
                                ["Content-Encoding: " , What, "\r\n"]});
1656 1694
 accumulate_header({"Content-Encoding", What}) ->
1657 1695
     accumulate_header({content_encoding, What});
@@ -1662,6 +1700,7 @@ accumulate_header({content_length, Len}) when is_integer(Len) ->
1662 1700
                 chunked = false,
1663 1701
                 transfer_encoding = undefined,
1664 1702
                 contlen = Len,
  1703
+                act_contlen = 0,
1665 1704
                 content_length = make_content_length_header(Len)});
1666 1705
 accumulate_header({"Content-Length", Len}) ->
1667 1706
     case Len of
@@ -1671,6 +1710,20 @@ accumulate_header({"Content-Length", Len}) ->
1671 1710
             accumulate_header({content_length, list_to_integer(L)})
1672 1711
     end;
1673 1712
 
  1713
+accumulate_header({transfer_encoding, What}) ->
  1714
+    put(outh, (get(outh))#outh{chunked = true,
  1715
+                               contlen = 0,
  1716
+                               transfer_encoding =
  1717
+                                   ["Transfer-Encoding: " , What, "\r\n"]});
  1718
+accumulate_header({"Transfer-Encoding", What}) ->
  1719
+    accumulate_header({transfer_encoding, What});
  1720
+
  1721
+accumulate_header({www_authenticate, What}) ->
  1722
+    put(outh, (get(outh))#outh{www_authenticate =
  1723
+                                   ["WWW-Authenticate: " , What, "\r\n"]});
  1724
+accumulate_header({"WWW-Authenticate", What}) ->
  1725
+    accumulate_header({"WWW-Authenticate", What});
  1726
+
1674 1727
 %% non-special headers (which may be special in a future Yaws version)
1675 1728
 
1676 1729
 accumulate_header({Name, What}) when is_list(Name) ->
@@ -1705,14 +1758,38 @@ split_header([C|S], A) ->
1705 1758
 
1706 1759
 erase_header(connection) ->
1707 1760
     put(outh, (get(outh))#outh{connection = undefined, doclose = false});
  1761
+erase_header(server) ->
  1762
+    put(outh, (get(outh))#outh{server = undefined});
1708 1763
 erase_header(cache_control) ->
1709 1764
     put(outh, (get(outh))#outh{cache_control = undefined});
  1765
+erase_header(expires) ->
  1766
+    put(outh, (get(outh))#outh{expires = undefined});
  1767
+erase_header(date) ->
  1768
+    put(outh, (get(outh))#outh{date = undefined});
  1769
+erase_header(allow) ->
  1770
+    put(outh, (get(outh))#outh{allow = undefined});
  1771
+erase_header(last_modified) ->
  1772
+    put(outh, (get(outh))#outh{last_modified = undefined});
  1773
+erase_header(etag) ->
  1774
+    put(outh, (get(outh))#outh{etag = undefined});
1710 1775
 erase_header(set_cookie) ->
1711 1776
     put(outh, (get(outh))#outh{set_cookie = undefined});
  1777
+erase_header(content_range) ->
  1778
+    put(outh, (get(outh))#outh{content_range = undefined});
  1779
+erase_header(content_length) ->
  1780
+    put(outh, (get(outh))#outh{contlen = 0,
  1781
+                               content_length = undefined});
1712 1782
 erase_header(content_type) ->
1713 1783
     put(outh, (get(outh))#outh{content_type = undefined});
1714 1784
 erase_header(content_encoding) ->
1715  
-    put(outh, (get(outh))#outh{content_encoding = undefined});
  1785
+    put(outh, (get(outh))#outh{encoding = identity,
  1786
+                               content_encoding = undefined});
  1787
+erase_header(transfer_encoding) ->
  1788
+    put(outh, (get(outh))#outh{chunked = false,
  1789
+                               act_contlen = 0,
  1790
+                               transfer_encoding = undefined});
  1791
+erase_header(www_authenticate) ->
  1792
+    put(outh, (get(outh))#outh{www_authenticate = undefined});
1716 1793
 erase_header(location) ->
1717 1794
     put(outh, (get(outh))#outh{location = undefined}).
1718 1795
 
@@ -1988,6 +2065,9 @@ http_collect_headers(CliSock, Req, H, SSL, Count) when Count < 1000 ->
1988 2065
         {ok, {http_header, _Num, 'Content-Type', _, X}} ->
1989 2066
             http_collect_headers(CliSock, Req,
1990 2067
                                  H#headers{content_type = X},SSL, Count+1);
  2068
+        {ok, {http_header, _Num, 'Content-Encoding', _, X}} ->
  2069
+            http_collect_headers(CliSock, Req,
  2070
+                                 H#headers{content_encoding = X},SSL, Count+1);
1991 2071
         {ok, {http_header, _Num, 'Transfer-Encoding', _, X}} ->
1992 2072
             http_collect_headers(CliSock, Req,
1993 2073
                                  H#headers{transfer_encoding=X},SSL, Count+1);
10  src/yaws_api.erl
@@ -1148,6 +1148,11 @@ reformat_header(H) ->
1148 1148
                  true ->
1149 1149
                       {"Content-Type", H#headers.content_type}
1150 1150
               end,
  1151
+              if H#headers.content_encoding == undefined ->
  1152
+                      undefined;
  1153
+                 true ->
  1154
+                      {"Content-Encoding", H#headers.content_encoding}
  1155
+              end,
1151 1156
 
1152 1157
               if H#headers.authorization == undefined ->
1153 1158
                       undefined;
@@ -1163,6 +1168,11 @@ reformat_header(H) ->
1163 1168
                       undefined;
1164 1169
                  true ->
1165 1170
                       {"Location", H#headers.location}
  1171
+              end,
  1172
+              if H#headers.x_forwarded_for == undefined ->
  1173
+                      undefined;
  1174
+                 true ->
  1175
+                      {"X-Forwarded-For", H#headers.x_forwarded_for}
1166 1176
               end
1167 1177
 
1168 1178
              ]

0 notes on commit e61b0e3

Please sign in to comment.
Something went wrong with that request. Please try again.