基於visual c++之windows核心編程程式碼分析(55)使用GoogleUrl方便安全地解析URL

來源:互聯網
上載者:User

我們在進行google搜尋的時候,google為了安全起見,經常對於URL通過Google的安全過濾,以過濾網路木馬,如

 

 

下面我們來親自實踐,基於VC++來實現使用GoogleUrl方便安全地解析URL

#include "stdafx.h"#include "googleurl\src\gurl.h"void testURL(const char* lpszUrl){char szDomain[256];char szUrlPath[256];szDomain[0] = 0;szUrlPath[0] = 0;const char *szStart = NULL;const char *szPathStart = NULL;if (strnicmp(lpszUrl, "http://", 7) == 0){//http開頭szStart = lpszUrl + 7;} else if (strnicmp(lpszUrl, "https://", 8) == 0) {//https開頭szStart = lpszUrl + 8;} else {szStart = lpszUrl;}szPathStart = strstr(szStart, "/");if (szPathStart){strcpy(szUrlPath, szPathStart);}strncpy(szDomain, szStart, szPathStart - szStart);szDomain[szPathStart - szStart] = 0;printf("%s\nDomain: %s\nPath: %s\n", lpszUrl, szDomain, szUrlPath);}int _tmain(int argc, _TCHAR* argv[]){std::cout << std::endl << "testURL" << std::endl;testURL("http://www.baidu.com/index.html");//testURL("http://www.ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ.com/index.html?ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");//GoogleUrlstd::cout << std::endl << "GoogleUrl" << std::endl;GURL gurl("http://user:123456@foo.tengattack.com:8080/index.php?action=init");std::cout<< "URL: " << gurl << std::endl << std::endl;//得到網域名稱\主機地址std::cout << "host() -> " << gurl.host() << std::endl;//得到連接埠號碼if (gurl.has_port())std::cout << "port() -> " << gurl.port() << std::endl;//得到queryif (gurl.has_query())std::cout << "query() -> " << gurl.query() << std::endl;//得到使用者名稱密碼if (gurl.has_username())std::cout << "username() -> " << gurl.username() << std::endl;if (gurl.has_password())std::cout << "password() -> " << gurl.password() << std::endl;//得到檔案名稱std::cout << "ExtractFileName() -> " << gurl.ExtractFileName() << std::endl;//這個也是連接埠號碼,與port()的區別就是就算沒有指定連接埠號碼預設會返回HTTP 80連接埠std::cout << "EffectiveIntPort() -> " << gurl.EffectiveIntPort() << std::endl;//判斷網域名稱是否為IP地址std::cout << "HostIsIPAddress() -> " << gurl.HostIsIPAddress() << std::endl;std::cin.get();return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.