设为首页 收藏本站
查看: 905|回复: 0

[经验分享] [转]关于Apache的内容协商

[复制链接]

尚未签到

发表于 2017-1-6 07:17:07 | 显示全部楼层 |阅读模式
  该功能使得服务器可以根据agent指定的http头来选择合适的资源。

涉及的http头包括: Accept-*

涉及的Apache中的知识: 类型表


1。 需要模块 : modules/mod_negotiation.so

2。 需要在目录的Options中添加: MultiViews;  如: Options FollowSymLinks MultiViews Indexes

3。 参考文档: http://apache.jz123.cn/content-negotiation.html

4。 该功能可能会影响到rewrite,参考: http://www.linuxpk.com/4941.html


apache根据你给的资源名称a,查找所有的a.*资源,加入有两种资源: a.txt 和a.php, 在类型表中查出:

.txt 对应文档类型为: text/plain 

.php 对应文档类型为: application/x-httpd-php


如果请求时使用的 accept为:
text/plain ,则返回a.txt

如果请求时使用的 accept为:
application/x-httpd-php
,则返回a.php

如果请求时使用的accept为: text/none ,找不到这种类型,则协商失败,apache返回406,并返回所有可用的类型列表,如:




Not Acceptable
  An appropriate representation of the requested resource /a could not be found  on this server.
  Available variants:


        

  • a.php
    , type application/x-httpd-php
        

  • a.txt
    , type text/plain
  Apache可以协商的内容基本有四类:


  • 文档类型: content-type, 通过accept来说明
  • 语言: language, 通过accept-language来说明
  • 字符集: charset, 通过accept-charset来说明
  • 编码: encoding, 通过accept-encoding来说明; (注意是传输过程的编码,不是字符的编码)
  相关源码参考:

modules/mappers/mod_negotiation.c





  • typedef

    struct
    {


  •     apr_pool_t *pool;

  •     request_rec *r;

  •     neg_dir_config *conf;

  •     char
    *dir_name;


  •     int
    accept_q;              
    /* 1 if an Accept item has a q= param */


  •     float
    default_lang_quality;
    /* fiddle lang q for variants with no lang */



  •     /* the array pointers below are NULL if the corresponding accept


  •      * headers are not present

  •      */

  •     apr_array_header_t *accepts;            /* accept_recs */


  •     apr_array_header_t *accept_encodings;   /* accept_recs */


  •     apr_array_header_t *accept_charsets;    /* accept_recs */


  •     apr_array_header_t *accept_langs;       /* accept_recs */



  •     apr_array_header_t *avail_vars;         /* available variants */



  •     int
    count_multiviews_variants;    
    /* number of variants found on disk */



  •     int
    is_transparent;      
    /* 1 if this resource is trans. negotiable */



  •     int
    dont_fiddle_headers;  
    /* 1 if we may not fiddle with accept hdrs */


  •     int
    ua_supports_trans;    
    /* 1 if ua supports trans negotiation */


  •     int
    send_alternates;      
    /* 1 if we want to send an Alternates header */


  •     int
    may_choose;          
    /* 1 if we may choose a variant for the client */


  •     int
    use_rvsa;            
    /* 1 if we must use RVSA/1.0 negotiation algo */


  • } negotiation_state;



typedef struct {     apr_pool_t *pool;     request_rec *r;     neg_dir_config *conf;     char *dir_name;     int accept_q;               /* 1 if an Accept item has a q= param */     float default_lang_quality; /* fiddle lang q for variants with no lang */      /* the array pointers below are NULL if the corresponding accept      * headers are not present      */     apr_array_header_t *accepts;            /* accept_recs */     apr_array_header_t *accept_encodings;   /* accept_recs */     apr_array_header_t *accept_charsets;    /* accept_recs */     apr_array_header_t *accept_langs;       /* accept_recs */      apr_array_header_t *avail_vars;         /* available variants */      int count_multiviews_variants;    /* number of variants found on disk */      int is_transparent;       /* 1 if this resource is trans. negotiable */      int dont_fiddle_headers;  /* 1 if we may not fiddle with accept hdrs */     int ua_supports_trans;    /* 1 if ua supports trans negotiation */     int send_alternates;      /* 1 if we want to send an Alternates header */     int may_choose;           /* 1 if we may choose a variant for the client */     int use_rvsa;             /* 1 if we must use RVSA/1.0 negotiation algo */ } negotiation_state;



  对于协商的表达方式都是一样的,如:

Accept: */*

Accept-Language: zh-cn,zh;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7

  其中“,”和“;”的分隔或许不太好明白其含义,其实,其格式是这样的:

name;q=N;charset=TEXT

如果要表达多组,则用“,”分隔;如:

name;q=N;charset=TEXT,name;q=N;charset=TEXT


其中,q、charset都是可以省略的,如:

Accept: */*

只有一组说明,而且是省略了q和charset



相关源码参考:

modules/mappers/mod_negotiation.c





  • typedef

    struct
    accept_rec {


  •     char
    *name;                
    /* MUST be lowercase */


  •     float
    quality;


  •     float
    level;


  •     char
    *charset;              
    /* for content-type only */


  • } accept_rec;



typedef struct accept_rec {     char *name;                 /* MUST be lowercase */     float quality;     float level;     char *charset;              /* for content-type only */ } accept_rec;



  

关于文档类型的协商依赖的是: docs/conf/mime.types

如:

文档类型                     资源扩展名

text/html                   html htm

text/css                    css

text/plain                  txt text conf def list log in  


关于语言和字符集的协商依赖的是: docs/conf/charset.conv

如:

# Lang-abbv Charset     Language

#---------------------------------

en          ISO-8859-1  English

UTF-8       utf8        UTF-8  

Unicode     ucs         Unicode

th          Cp874       Thai    

ja          SJIS        Japanese

ko          Cp949       Korean  

zh          Cp950       Chinese-Traditional

zh-cn       GB2312      Chinese-Simplified

zh-tw       Cp950       Chinese

。。。


其中,第一列是语言的缩写,协商时一般用缩写; 第二列是字符集



---------------------------

内容可能是根据多个条件来协商的,那么对于一个协商的资源可能涉及到多个扩展名的,如:

content.en.html.gz

该资源如果写成了:

content.html.en.gz

也是可以找到的,只是在做超链接的时候,如果写成了: content.gz.html.en 就找不到了

参考: http://httpd.apache.org/docs/2.2/content-negotiation.html#naming

关于内容协商与cache


对于http1.0来讲,经过协商的内容是不建议cache的;在http1.1中添加了vary的http头,用来告知客户端内容是根据哪些条件来协商的,这样客户端可以尽可能的利用cache,如果协商条件不变的话就可以使用cache的。


====================

参考资料:


http://httpd.apache.org/docs/2.2/content-negotiation.html

http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html

  原文地址:http://phpor.net/blog/post/786/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-324399-1-1.html 上篇帖子: Apache有名的几个项目介绍 下篇帖子: Apache OpenJPA 2.1.0 发布
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表