Custom HTTP packet capture and filtering

Source: Internet
Author: User
Custom HTTP packet capture and filtering define an http packet capture class, send data to a custom accept script, you can send successfully, and receive data, but not to the Internet, analyzed the request header information when an HTTP request is sent in the browser. by simulating the request, the request timed out...

// Define an HTTP packet capture class. you can also use curl .....
 HostInfo = parse_url ($ url); $ this-> setRequestHeader (array ('host' => $ this-> hostInfo ['host']); $ this-> setRequestHeader (array ('connection' => 'keep-alive');} // sets the HTTP request line information, for example: GET/resources HTTP/1.1 // but to avoid missing the url? The start query information. it is necessary to determine the public function setRequestLine ($ method) {// if it is a POST request, the content-type header information if (strtolower ($ method) is automatically added) = 'post') {$ this-> setRequestHeader (array ('content-type' => 'application/x-www-form-urlencoded');} if (! Empty ($ this-> hostInfo ['query']) {$ this-> requestLine = strtoupper ($ method ). "". $ this-> hostInfo ['path']. "? ". $ This-> hostInfo ['query']. "HTTP/1.1 \ r \ n";} else {$ this-> requestLine = strtoupper ($ method ). "". $ this-> hostInfo ['path']. "HTTP/1.1 \ r \ n" ;}// sets the HTTP request header. // The receiving parameter is of the array type. key: value is spliced through iteration, and the public function setRequestHeader ($ header) {foreach ($ header as $ key => $ value) is wrapped in a new line) {$ this-> requestHeader. = $ key. ":". $ value. "\ r \ n" ;}/// set the HTTP request body // The receiving parameter is of the array type, and key = value is spliced through iteration, because the last part of the stitching has a redundant &, it is necessary to remove the public function setRequestBody ($ body) {foreach ($ body as $ key => $ value) {$ this-> requestBody. = $ key. '= '. $ value. '&';} $ offset = strrpos ($ this-> requestBody, '&'); $ this-> requestBody = substr ($ t His-> requestBody, 0, $ offset);} // assemble the request line, request header, and request body. based on the length of the request body, the public function setRequestEntity () {if (! Empty ($ this-> requestBody) {$ contentLength = strlen ($ this-> requestBody ); $ this-> setRequestHeader (array ('content-length' => $ contentLength); $ this-> requestEntity = $ this-> requestLine. $ this-> requestHeader. "\ r \ n ". $ this-> requestBody;} else {$ this-> requestEntity = $ this-> requestLine. $ this-> requestHeader. "\ r \ n" ;}}// specifies the host name resolution function ....... public function parseHost ($ url) {$ pat = '# http: // ([^/] +) # I'; if (preg_match ($ pat, $ url, $ match) {return $ match [1];} else {echo 'failed to match host information
';}} // Create a connection to the host public function createConnect () {$ this-> connect = fsockopen ($ this-> hostInfo ['host'], 80, $ this-> errNo, $ this-> errStr) or die ('host connection failed '. $ this-> errStr);} // send the public function sendRequest () {$ this-> setRequestEntity (); echo $ this-> requestEntity; exit (); $ this-> createConnect (); $ entityLength = strlen ($ this-> requestEntity); if ($ entityLength! = Fwrite ($ this-> connect, $ this-> requestEntity, $ entityLength) {die ('data writing failed
');} Else {$ this-> receiveResponse () ;}// receives the request and concatenates the public function receiveResponse () {while (! Feof ($ this-> connect) {$ this-> responseEntity. = fread ($ this-> connect, 1024) ;}// location of the empty line between the calculation response header and the response body public function calculateEmptyLinePos () {$ this-> emptyLinePos = strpos ($ this-> responseEntity, "\ r \ n", 0 );} // accept the response body header .... public function receiveResponseHeader () {$ this-> calculateEmptyLinePos (); $ this-> responseHeader = substr ($ this-> responseEntity, 0, $ this-> emptyLinePos ); echo $ this-> responseHeader;} // public function receiveResponseBody () {$ this-> calculateEmptyLinePos (); $ this-> responseBody = substr ($ this-> responseEntity, $ this-> emptyLinePos);} // return the request result public function getResponse () {return $ this-> responseEntity;} public function parseResponse () {} public function _ destruct () {// fclose ($ this-> connect );}} set_time_limit (60); $ http = new Httpwrap (" http://www.mmkao.com/Beautyleg/ "); // Set the HTTP request line $ http-> setRequestLine (" get "); // Set the HTTP header $ http-> setRequestHeader (array ("Accept" => "text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8 "); $ http-> setRequestHeader (array (" Accept-Language "=>" zh-cn, zh; q = 0.8, en-us; q = 0.5, en; q = 0.3 "); $ http-> setRequestHeader (array (" Accept-Encoding "=>" gzip, deflate ")); $ http-> setRequestHeader (array ("User-Agent" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) chrome/38.0.2125.101 Safari/537.36 "); // $ http-> setRequestHeader (array (" Cookie "=>" BAIDU_DUP_lcr = http://www.baidu.com/s?wd=beautyleg&rsv_spt=1&issp=1&f=3&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=6&rsv_sug4=415&rsv_sug1=3&oq=beauty&rsv_sug2=0&rsp=0&inputT=2363 ; Safedog-flow-item = 8471BA510DA33350ED344AC374D3044A; bd1__firstime = 1415165097782; cscpvrich_fidx = 6; AJSTAT_ OK _pages = 2; AJSTAT_ OK _times = 2; cnzzdata3838523 = cnzz_eid % %%253a % 252F % 252Fwww.baidu.com % 252F % 26 ntime % 3D1415169712 "); // send data $ http-> sendRequest (); // $ http-> receiveResponseHeader ();?> This class is used to get a custom script that can send and receive data. the script is as follows: However, when sending a request to this website, the request times out: The website is: http://www.mmkao.com/Beautyleg/ Header information when chrome sends a request to the homepage of the website: Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, */*; q = 0.8Accept-Encoding: gzip, deflate, sdchAccept-Language: zh, en; q = 0.8, zh-TW; q = 0.6, zh-CN; q = 0.4, ja; q = 0.2Cache-Control: max-age = 0 Connection: keep-aliveCookie: BAIDU_DUP_lcr = http://www.baidu.com/s?wd=beautyleg&rsv_spt=1&issp=1&f=3&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=6&rsv_sug4=415&rsv_sug1=3&oq=beauty&rsv_sug2=0&rsp=0&inputT=2363 ; Safedog-flow-item = 8471BA510DA33350ED344AC374D3044A; bd1__firstime = 1415165097782; cscpvrich_fidx = 7; AJSTAT_ OK _pages = 3; AJSTAT_ OK _times = 2; cnzzdata3821323 = cnzz_eid % 3D253823549-1415164312-http % 253A % 252F % 252Fwww.baidu.com % 252F % 26 ntime % 3D1415169712DNT: 1 Host: www. mmkao. comUser-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36 Response Headersview source // use the same packaging, when the Httpwrap request is called to send a request, the system prompts a timeout. the error is caused by an unknown error ........ for this website, I wrote a class to filter out image links: ResponseBody = $ body;} // the url that matches the image src. public function feedImage () {$ pat = '# imgLink [] = $ link ;}} else {echo 'failed to match the image link address '."
";}}// Extract part of the head public function filterHeader ($ body) {$ pat = '#[\ S \ S] +# Im '; if (preg_match ($ pat, $ body, $ match) {$ this-> header = $ match [0];} else {echo 'failed to match the head part '."
";}}// Extract the public function filterBody ($ body) {$ pat = '#[\ S \ S] +# Im '; if (preg_match ($ pat, $ body, $ match) {$ this-> body = $ match [0];} else {echo 'failed to match the body part '."
";}}// Extract the page information. this can only be matched in a targeted manner. public function rollPage ($ body) cannot be used) {$ pat = '# [\ x {4e00}-\ x {9fa5}] + \ s * \ d \ s +? /\ S +? \ D + \ s * [\ x {4e00}-\ x {9fa5}] * # ui '; if (preg_match ($ pat, $ body, $ match )) {$ patNum = '#/\ s * (\ d *) #'; if (preg_match ($ patNum, $ match [0], $ num )) {$ this-> pageNum = $ num [1];} else {echo 'failed to extract the specific paging value '."
";}} Else {echo 'failed to extract paging Statistics '."
";}}?> Note: both classes have passed the intranet test and successfully filtered out the image links ......


Reply to discussion (solution)

In the window command line, submit a request to receive data ......


Data received

$url = 'http://www.mmkao.com/Beautyleg/';print_r(get_headers($url));
Array(    [0] => HTTP/1.1 200 OK    [1] => Connection: close    [2] => Date: Wed, 05 Nov 2014 08:53:09 GMT    [3] => Content-Length: 13889    [4] => Content-Type: text/html    [5] => Content-Location: http://www.mmkao.com/Beautyleg/index.html    [6] => Last-Modified: Wed, 05 Nov 2014 05:39:09 GMT    [7] => Accept-Ranges: bytes    [8] => ETag: "e8939ad2baf8cf1:693"    [9] => Server: IIS    [10] => X-Powered-By: WAF/2.0    [11] => Set-Cookie: safedog-flow-item=8471BA510DA33350ED344AC374D3044A; expires=Sat, 12-Dec-2150 10:26:25 GMT; domain=mmkao.com; path=/)

$url = 'http://www.mmkao.com/Beautyleg/';print_r(get_headers($url));
Array(    [0] => HTTP/1.1 200 OK    [1] => Connection: close    [2] => Date: Wed, 05 Nov 2014 08:53:09 GMT    [3] => Content-Length: 13889    [4] => Content-Type: text/html    [5] => Content-Location: http://www.mmkao.com/Beautyleg/index.html    [6] => Last-Modified: Wed, 05 Nov 2014 05:39:09 GMT    [7] => Accept-Ranges: bytes    [8] => ETag: "e8939ad2baf8cf1:693"    [9] => Server: IIS    [10] => X-Powered-By: WAF/2.0    [11] => Set-Cookie: safedog-flow-item=8471BA510DA33350ED344AC374D3044A; expires=Sat, 12-Dec-2150 10:26:25 GMT; domain=mmkao.com; path=/)



This is true. It hurts. let me see it again.

GET/Beautyleg/HTTP/1.1
Host: www.mmkao.com
Connection: keep-alive
Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8
Accept-Language: zh-cn, zh; q = 0.8, en-us; q = 0.5, en; q = 0.3
Accept-Encoding: gzip, deflate

HTTP/1.1 200 OKDate: Wed, 05 Nov 2014 09:34:02 GMTContent-Length: 13889Content-Type: text/htmlContent-Location: drawing Wed, 05 Nov 2014 05:39:09 GMTAccept-Ranges: bytesETag: "e8939ad2baf8cf1: 693 "Server: IISX-Powered-By: WAF/2.0Set-Cookie: safedog-flow-item = Shanghai; expires = Sat, 12-Dec-2150 11:07:18 GMT; domain = mmkao.com; path =/
 Beautyleg _ Mimi tuxiu
 
 ............................
 
 
    A total of 1035 groups, 30 groups per page, current 1/35 pages. The last page of the previous page. Go to

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.