Application Layer: Solutions in PHP fine stream, reciprocity true friends HTTP / HTTPS / RTMP

Friends of friends is that two strings with wire strung shells (monetary ancient times), junior partner to your house guest at the earliest source Oracle, please come up with a bunch of shells friends to dinner, you go home stopping partner, partner out a bunch of shells you eat, the emphasis is on reciprocity.

http protocol as the principle of reciprocity between friends, a request (request) and response (response) configuration, an application layer protocol, a standard client-server model. http protocol carried over TCP.

Request and response

Request: connecting loop is actually a transmission layer, which is established between two applications communicate with each other. Request consists of three parts: the request line, header message, request body. Important header information in the header below.

Response: After receiving and re-request message, http server returns a response message. http response is also composed of three parts, namely: a status line, a message packet, the response body.

1xx - 指示信息 - 请求已接收,继续处理。
2xx - 成功 - 请求已被成功接收、理解、接收
3xx - 重定向 - 要完成请求必须进行进一步的操作
4xx - 客户端错误 - 请求有语法错误或请求无法实现
5xx - 服务器错误 - 服务器未能实现合法的请求

According to experience personal mobility server 403 may nginx index has not specified the default file, there may be 502 configuration file error, resulting in response times out of 500 possible causes of the problem caused by DNS.

Http important header information

I believe one thing or one person, mainly to see the person's property. For example, efforts to make progress, sunshine, clean, gentle, loving, and so on, http header information is the same, with a few important header information index.

  • Host: mainly used to specify the Internet host and port number of the requested resource, namely the domain name, usually extracted from the url out, the default port is 80, you can specify the port.
  • User-Agent: referred to as UA, the content requesting information comprises a user. UA head already is an important indicator to identify the device.
  • Accept: tell the server can accept the file format, receiving wap may be less.
  • Cookie: Cookie two types, one is the client as sent by the server, using the Cookie header, used to mark some of the information; the other is the server to the browser header to set Cookie. The main difference between the two is in advance Cookie reported a record can have more value.
  • Cache-Control: Specifies the request and response caching mechanism to follow.
  • Referrer: header field allows the client to specify the source address of the request URI of the resource, which allows the server to generate a return linked list can be used to login to optimize caching.
  • Content-Encoding: Specifies the encoding acceptable.

HTTPS

Before says https, http belong to a stateless protocol, in the browser are transmitted in the clear, in order to prevent information leakage and tampering, guarantee the security of information transmission, heroes have transmitted data is encrypted. There are symmetric encryption (DES), asymmetric encryption (RSA), and the digital signature / certificate (CA).

A friend of mine there will be secret, and some secrets you just want to let a friend know certain confidentiality of everyone else. At time to the computer's information is not hijacked and stolen, there https protocol, essentially adding a protective layer on the SSL, port 443.

Generates the corresponding service providers in the corresponding public key and a private key, a.key and a.pem, open port 443 on the server, configured on nginx, https principle of many, we do not know, I did not dare Xia Xie friends.

server {
    listen 443;
    server_name wap.xxxx.com;
    ssl on;
    root /home/xxxxx;
    index index.html index.htm index.php;
    ssl_certificate   ../cert/a.pem;
    ssl_certificate_key  ../cert/a.key;
    ...
}

RTMP Streaming

In recent years from the development of the media, a variety of small live full swing, live happy to leave you, research the code, leaving us, the best study notes, leaving my beloved readers, column Title III, cut write and intentions.

How to package the data stream transmitted to the peer?

This format is that it can not be transmitted directly to the Internet to end, going live it? Actually not, we need to package the binary stream into network packets sent, here we use RTMP protocol. It entered the second process, push the stream.

RTMP 是基于 TCP 的,因而肯定需要双方建立一个 TCP 的连接。在有 TCP 的连接的基础上,还需要建立一个 RTMP 的连接,也即在程序里面,你需要调用 RTMP 类库的 Connect 函数,显示创建一个连接。

RTMP 为什么需要建立一个单独的连接呢?

因为它们需要商量一些事情,保证以后的传输能正常进行。主要就是两个事情,一个是版本号,如果客户端、服务器的版本号不一致,则不能工作。另一个就是时间戳,视频播放中,时间是很重要的,后面的数据流互通的时候,经常要带上时间戳的差值,因而一开始双方就要知道对方的时间戳。

握手之后,双方需要互相传递一些控制信息,真正传输数据的时候,还是需要创建一个流 Stream,然后通过这个 Stream 来推流 publish。推流的过程,就是将 NALU 放在 Message 里面发送,这个也称为RTMP Packet 包。前面连接的时候,设置的 Chunk 块大小就是指这个 Chunk。将大的消息变为小的块再发送,可以在低带宽的情况下,减少网络拥塞。

就这样数据就源源不断到达流媒体服务器,整个过程就像这样。

PHP中的流

之前去小米面试,面试官问了一句了解的流函数,问的我是一脸懵逼,之后再后来的工作里也遇见了相同的一些问题,下面我们就来梳理php中的流,介绍 php://input 和 stream_* 系列函数。

所谓php中的流,每一种流都实现了一个包装器(wrapper),包装器包含一些额外的代码用来处理特殊的协议和编码。PHP提供了一些内置的包装器,我们也可以很轻松的创建和注册自定义的包装器。我们甚至可以使用上下(contexts)和过滤器来改变和增强包装器。

 // 服务端代码	
 $data = file_get_contents('php://input');
 var_dump( $data );
 var_dump($_POST);
 var_dump($_FILES);

Curl

curl http://blog.zhuangbfan.com/input.php?php=2  
curl http://blog.zhuangbfan.com/input.php?php=2 -d "name=zhang&age=18"
curl http://blog.zhuangbfan.com/input.php?php=2 -F "file=@/Users/stark/Documents/wechat01.png" -i

curl -i #输出请求和响应的详细信息
curl -d #发送POST数据
curl -H #携带头信息
curl -F #发送文件 

亲测,php://input 流中只能接收POST原始信息, F I L E S , _FILES, _GET,都接收不到,数据以原始流的数据方式显示。

[root@iZ2ze9gei ~]# curl http://blog.zhuangbfan.com/input.php -d "name=zhang&age=18" -i
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 23 Jul 2019 01:59:00 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.0.30

string(17) "name=zhang&age=18"

谢谢你的阅读。

EN。

发布了98 篇原创文章 · 获赞 185 · 访问量 9万+

Guess you like

Origin blog.csdn.net/xuezhiwu001/article/details/96971565