psr-7-从HTTP消息到PSR-7:它的全部含义是什么_第1页
psr-7-从HTTP消息到PSR-7:它的全部含义是什么_第2页
psr-7-从HTTP消息到PSR-7:它的全部含义是什么_第3页
psr-7-从HTTP消息到PSR-7:它的全部含义是什么_第4页
psr-7-从HTTP消息到PSR-7:它的全部含义是什么_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、sr-7_HTTPPSR-7:它的全部含义是什么?psr-7This article was peer reviewed by . Thanks to all of SitePoints peer reviewers for making SitePoint content the best it canbe!本由同评审。 感谢所有SitePoint的同评审员使SitePoint内容达到最佳状态!The PHP Framework Interoperability Group (PHP-FIG) has relatively recently approved another proposal

2、, the . The documentcrystallizes HTTP messages into 7 interfaces which a PHP library should implement if they subscribe to the specification. In, Matthew Weier OPhinney, editor of the PSR, gives an interesting overview of the specification. So what is it?PHP框架互操作性组(PHP-FIG)较近期批准了另项建议,即 。 该档将HTTP消息具体

3、化为7个接,如果PHP订阅该规范,则PHP库应该实现这些接。 在 ,PSR的编辑Matthew Weier OPhinney对该规范进了有趣的概述。 那是什么If you typebbc.co.uk in your browser, you go straight to the homepage of the BBC,but a number of things might have takenplace between the time the browser sent an HTTP request to the server and it getting a response back.

4、如果在浏览器中键bbc.co.uk ,则直接转到BBC的主页,但是在浏览器向服务器发送HTTP请求到它返回响应之间可能发了很多事情。Heres a sample raw request.这是原始请求例。Accept: */*Referer:Its always made up of a request line (GET / HTTP/1.1), a number of header field lines listed as: , a single emptyline and an optional body for any data payload (for example query p

5、arameters or post data).它始终由请求(GET / HTTP / 1.1),列为: 的多个标头字段,单个空和任何数据有效负载(例如查询参数或后置数据)的可选主体组成)。The blank line after the zero or more header lines must be a CRLF by itself. This means 2 characters an ASCII 13(Carriage Return), followed by ASCII 10 (Line Feed) or.零个或多个标题之后的空本必须是CRLF。 这意味着2个字符-ASCII13(

6、回车),然后是ASCII10(换)或rn。Lets send this request from the command line via curl and see the response:让我们通过curl从命令发送此请求,并查看响应:curl -i -H Host: bbc.co.uk -H User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) -H Accept: */* -X GET http:/bbc.co.ukDate: Sun, 02 Oct 2016 20:49:42 GMTLo

7、cation: http:/www.bbc.co.uk/Connection: Keep-AliveContent-Length: 0curl -i -H Host: www.bbc.co.uk -H User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) -H Accept: */* -X GET http:/www.bbc.cServer: nginxContent-Type: text/html; charset=utf-8ETag: W/29c44-MXo+6t7MoPRad358MSWqw

8、AX-Frame-Options: SAMEORIGINx-origin-route: xrt-extContent-Length: 171076Date: Sun, 02 Oct 2016 20:54:27 GMTConnection: keep-aliveSet-Cookie: BBC-UID=15c73fe11704a0731344da5ec13869204c1a22a0c7b444d60a708762e631ac0c0Mozilla/5.0%20(compatible%3b%20MSIE%209.0%3b%20X-Cache-Action: HITX-Cache-Hits: 1223X

9、-Cache-Age: 55!-Thats more like it. The first line,HTTP/1.1 200 OK, is the status line. Then we have headers, in a similar pattern to requests: , an empty line, and the response body. Note: Wepassed the output throughless so that we can see the firstpart of the response.这还差不多。 HTTP/1.1 200 OK是状态。 然后

10、,我们具有与请求类似的头件-: value ,空和响应主体。 注意:我们将输出传递给less以便我们可以看到响应的第部分。Requests and responses can be broken down into a message line, a number of header lines and body lines. Thecommonalities can be abstracted in an interface( essageInterface) which the request RequestInterface) and response(ResponseInterface

11、) can extend with their distinct flavor of HTTP message.请求和响应可以分为消息,许多标题和正。 可以在接(MessageInterface)中抽象这些共性,请求(RequestInterface )和响应(ResponseInterface )可以使其独特的HTTP消息RequestInterface进扩展。PHP doesnt only run within a web environment, and web requests might have originated from APIs. TheServerRequestInter

12、face was designed to take care of other types of HTTP requests.PHP不仅在络环境中运,且络请求可能源API。ServerRequestInterface旨在处理其他类型的HTTP请求。The other three interfaces are a further abstraction of specific aspects in messages. Looking back at the request messageline:其他三个接是消息中特定的进步抽象。 回顾请求消息:GET / HTTP/1.1This compri

13、ses:这包括:METHOD:Although defines safe and idempotent types of methods, for general applications, its sufficient to identifythem by name GET, POST, PUT, PATCH,DELETE, OPTIONS, HEAD and TRACE.CONNECT is reserved for use with aproxy that can dynamically switch to being a tunnel e.g. SSL tunneling.法:尽管定义

14、了安全和幂等的法类型,但对于般应程序,通过名称来识别它们就够了-GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD和TRACE。 CONNECT保留供与代理起使,该代理可以动态切换为隧道,例如SSL隧道。TARGET: This is the URIor our request target, and things get a bit interesting here as we can have:标:这是URI或我们的请求标,在这,事情变得有些有趣,因为我们可以拥有:origin-form path and query string of the URI. The

15、query string may or may not be present.origin-form URI的路径和查询字符串。 查询字符串可能存在也可能不存在。absolute-form an absolute URI.绝对形式绝对URI。authority-form the authority part of a URI, made up of a maximum of 3 parts user-info (optional), host andport (optional). The user-info may require a password too user:password.

16、Weend up with a pattern ofuser:passwordhost:port. The user-info may also have require an权威形式 URI的权威部分,最多由3部分组成户信息(可选),主机和端(可选)。 户信息可能也需要密码 user:password。 我们以user:password host:port的模式结束。 户信息可能还需要个asterisk-form just the string, *星号形式仅是字符串,*Weend up withscheme:/user:passwordhost:port/path?query#fragme

17、nThis part of the request message wasabstracted to theUriInterface.我们以以下scheme:/user:passwordhost:port/path?query#fragment 束scheme:/user:passwordhost:port/path?query#fragment。 请求消息的这部分被抽象到UriInterface 。VERSION: Theres limited choice here as HTTP/1.1 is the current version. Prior to that, we had HTTP

18、/1.0, and the nextdraft version is HTTP/2.0版本:这的选择有限,因为HTTP / 1.1是当前版本。 在此之前,我们使HTTP / 1.0,下个草案版本是HTTP / 2.0Also, file uploads required special attention. In non-SAPI environments, $_FILES environment variable is empty and insome situations such as non-POST requests,$_FILES isnt populated at all. Th

19、eUploadedFileInterface was designed to allowfor a more consistent interaction with files.另外,件上传需要特别注意。 在SAPI环境中,$_FILES环境变量为空,在某些情况下(例如POST请求),根本不会填充$_FILES 。UploadedFileInterface旨在允许与件进更致的交互。The message (request or response) needs to be able to handle large data efficiently for both client and serv

20、er. PHP has hadstreams built in since 4.3.0. TheStreamInterfacepart of PSR-7 provides a wrapper around common operations andserialization of an entire stream to a string.消息(请求或响应)需要能够有效地为客户端和服务器处理量数据。 从4.3.0开始,PHP内置了流。 PSR-7的StreamInterface部分提供了通操作以及将整个流序列化为字符串的包装。挑战性 (Challenges)The path to PSR-7 w

21、as paved with strong debate and differing opinions at every step of the way.通往PSR-7的道路在每个步骤中都经过激烈的辩论和不同的意见。Immutable objects The immutability or mutability of objects was one of the most hotly-debated points, and PSR-7eventually settled for this:不可变的对象对象的不可变性或可变性是最热门的争论点之,PSR-7最终为此解决了:Messages are v

22、alues where the identity is the aggregate of all parts of the message; a change to any aspect of themessage is essentially a new message. This is the very definition of a value object. The practice by which changesresult in a new instance is termed immutability, and is a feature designed to ensure t

23、he integrity of a given value.消息是值,其中份是消息所有部分的总和; 对消息的任何进的更改本质上都是新消息。 这就是值对象的定义。 更改导致新实例的做法称为不变性,并且是旨在确保给定值完整性的功能。What this means is that each time you make any change to a message object, you end up with a new copy. Thecomplexity of URI, headers and streams require an assurance that a full adoption

24、 of immutability by all collaboratorsoffered to the designers of the interfaces.这意味着每次对消息对象进任何更改时,都会得到个新副本。 URI,标头和流的复杂性要求确保提供给接设计员的所有协作者都完全采不变性。With immutability, any state change you make requires you to assign the result.有了不变性,您所做的任何状态更改都需要您分配结果。$request =$request-setHeader(Cache-Control, public)

25、;Any method that changes the state of the current message returns an instance of it with the changes made.As long asa result assignment is made,you can chain any number of changes in a fluent-like syntax.更改当前消息状态的任何法都将返回所做更改的实例。 只要进了结果分配,就可以使流利的语法链接任意数量的更改。-setHeader(Cache-Control, public)-addHeader

26、(Cache-Control, max-age=18600)-setStatus(200);The methods having the prefix “with” must conform to the immutability requirement. A concern is that supportingmutability in one of the message interfaces means enforcing it across all the other interfaces.前缀为“ with”的法必须符合不变性要求。 个问题是,在消息接之中持可变性意味着要在所有其他接

27、上强制实现可变性。On the other hand, it has been highlighted that as they are generally thought to be. Its worthy of note that similarHTTP object implementations in Ruby and Node are mutable by design. So, PHP is in good company.另,已经强调了通常认为的 。 值得注意的是,Ruby和Node中类似的HTTP对象实现在设计上是可变的。 因此,PHP表现出。Nomenclature Thes

28、e objects are designed as interfaces. Isnt calling it MessageInterface superfluous? A methodsignature taking a request and response ends up being too long. Compare the following:命名法这些对象被设计为接。 这不是MessageInterface多余的吗? 接受请求和响应的法签名最终太长。 较以下内容:public function _invoke(ServerRequest $request, Response $re

29、sponse, callable $next) : ResponseAliasing is the suggested solution for those who prefer to drop the Interface suffixes.对于那些喜欢删除接后缀的户,建议使别名解决案。Middleware Weve got the message interfaces which are like the ingredients for making a cake, perfectly measuredout. I want a cake to consume but I dont know

30、 how to bake. How about a recipe? PSR-7 only prescribes a standard forthe definition of a request and response. How do we move from request to response? That “middleman” that doesthe leg work between the request and response is called middleware.中间件我们有消息接,就像制作蛋糕的原料样,经过精设计。 我想吃块蛋糕,但我不知道怎么烤。 谱怎么样? PSR

31、-7仅规定了定义请求和响应的标准。 我们如何从请求转移到响应? 在请求和响应之间起作的那个“中间”称为中间件。The next step would be a unification of how to plumb PSR-7 interfaces so that applications and frameworks thatconform to them can be completely swappable. This part of the debate which borders on providing a template for aninteroperable implemen

32、tation of PSR-7, has been moved to the separate PSR-15: HTTP Middlewares.下步将是如何使PSR-7接垂直化,以便符合它们的应程序和框架可以完全交换。 辩论的这部分与为PSR-7的互操作实现提供模板有关,已经转移到单独的PSR-15:HTTP中间件。法 (Usage)A number of libraries and frameworks have added support for PSR-7 in different ways.许多库和框架以不同的式增加了对PSR-7的持。1. Symfony The is one of

33、 the most popular OOP abstractions of the HTTP specification prior to PHP-FIG. With theemergence of PSR-7, Symfony opted for a which convertsHttpFoundation objects from and to objects which implementPSR-7 message interfaces.Symfony 是PHP-FIG之前HTTP规范中最流的OOP抽象之。 随着PSR-7的出现,Symfony选择了种 ,HttpFoundation将H

34、ttpFoundation对象与实现PSR-7消息接的对象相互转换。2. Zend Framework (ZF) They came up with a Composer package,endframework/zend-diactoros, of implementations ofthe HTTP message interfaces, not surprising as the editor of PSR-7 is the ZF Project Lead. goes a step further byincluding a server that is similar to the o

35、ne provided by in NodeJS.Zend Framework(ZF)他们提出了HTTP消息接实现的Composer软件包zendframework/zend-diactoros ,这并不奇怪,因为PSR-7的编辑者是ZF项负责。 通过在中包含与提供的服务器相似的服务器,进步了步。3. Slim describes a flexible approach that makes Slim accept any implementation of PSR-7. That is, if the one providedby Slim doesnt suffice.Slim 描述了种灵

36、活的法,可使Slim接受PSR-7的任何实现。 也就是说,如果Slim提供的功能还不够。4. Guzzle Being an HTTP client library, PSR-7 is of great relevance to this library. They built theguzzlehttp/psr7Composer package, a message implementation of PSR-7, which they rely on. gives an excellent overview of their takeon the specification.Guzzle

37、作为HTTP客户端库,PSR-7与该库常相关。 他们构建了guzzlehttp/psr7 Composer软件包,这是他们依赖的PSR-7的消息实现。 很好地概述了它们对规范的看法。5. Aura Theyve included , a web router implementation of PSR-7 in their collection of independent library packages.All router objects are managed by RouterContainer from which you retrieve an instance of aMap

38、object. Each methodof thisMap object may accept an HTTP message interface as one of its arguments.Aura 他们在独库包的集合中包含了 -7的Web路由器实现 。 所有路由器对象都由个RouterContainer管理,您可以从中检索Map对象的实例。 Map对象的每种法都可以接受HTTP消息接作为其参数之。6. HTTPlug A package of interfaces that allow library and application developers to create HTTP

39、clients that are fullycompatible with PSR-7. The HttpClient sends a PSR-7 Request and returns a PSR-7 response. See for a practical wayof using this package.HTTPlug 组接,允许库和应程序开发员创建与PSR-7完全兼容的HTTP客户端。 HttpClient发送PSR-7请求并返回PSR-7响应。 有关使此软件包的实法,请参阅 。Packagist has a with a wide range of popularity or ac

40、ceptance. However, a developer still has the following usage options:Packagist提供了并受到泛的欢迎或接受。 但是,开发员仍然具有以下使选项:1. Direct As the specification states, while PsrHttpMessageMessageInterface MAY be implemented directly,implementors SHOULD implement PsrHttpMessageRequestInterface and PsrHttpMessageResponse

41、Interface.The simplest way is to install the package with Composer.直接规范指出,虽然可以直接实现Psr Http Message MessageInterface,但是实现者应该实现Psr Http Message RequestInterface和Psr Http Message ResponseInterface。 最简单的法是使Composer安装软件包。composer require psr/http-message2. Indirect The interfaces are used indirectly through an adapter. For example, PSR-7 support in Symfony is throughthe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论