C++ libghttp库
2017-11-19 09:00:00 Author: payloads.online(查看原文) 阅读量:175 收藏

本文介绍一下C++下一个libghttp库的使用

0x00 安装

下载地址:https://github.com/Rvn0xsy/libghttp.git

git clone https://github.com/Rvn0xsy/libghttp.git
cd libghttp
./configure
make
make install

0x01 问题解决

checking host system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized


checking build system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized


checking for ranlib... ranlib
checking for ld used by GCC... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
updating cache ./config.cache
ltconfig: you must specify a host type if you use `--no-verify'
Try `ltconfig --help' for more information.
configure: error: libtool configure failed

即configure无法识别系统的类型, 所以提示you must specify a host type.

/usr/share/libtool/config/config.guess 覆盖源码包中的 config.guess

cp /usr/share/libtool/config/config.guess ./config.guess

/usr/share/libtool/config/config.sub 覆盖源码包中的 config.sub

cp /usr/share/libtool/config/config.sub ./config.sub

这样configure就可以猜出系统的类型了

如果/usr/share/libtool/config/config.guess 不存在则安装libtool,sudo apt-get install libtool

开始安装:

./configure --prefix=/usr/local
make 
sudo make install

0x02 异步请求

int ghttp_set_sync(ghttp_request *a_request,ghttp_sync_mode a_mode);

比普通模式要快

0x03 封装代码

CMakeLists.txt :

cmake_minimum_required(VERSION 3.8)
project(untitled1)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp http.h http.cpp)
INCLUDE_DIRECTORIES(
        /usr/local/include
)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/usr/local/lib")
INCLUDE_DIRECTORIES(/usr/local/include)
SET(CMAKE_EXE_LINKER_FLAGS
        "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,/usr/local/lib")
add_executable(untitled1 ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(untitled1 libghttp.a)

main.cpp :

#include <iostream>
#include <getopt.h>
#include <vector>
#include <string>
#include "http.h"
#include <sys/time.h>
int main() {
    struct timeval times;
    gettimeofday(&times,NULL);
    long int current_time = times.tv_sec;
    http  Req("/home/payloads/CLionProjects/untitled1/cmake-build-debug/url");
    if(Req.checkDic()==-1){
        std::cout << "Error .. " << std::endl;
        return -1;
    }
    Req.getUrlList();
    Req.Print();
    gettimeofday(&times,NULL);
    std::cout << "end " << times.tv_sec - current_time << std::endl;
    return 0;
}

http.h :

//
// Created by payloads on 11/19/17.
//
#ifndef UNTITLED1_HTTP_H
#define UNTITLED1_HTTP_H
#include <ghttp.h>
#include <string>
#include <vector>
struct requestResult{
    int status_code;
    std::string url;
    std::string server;
    std::string power;
    std::string title;
};
class http{
public:
    http(std::string urlists);
    ~http();
    void getUrlList();
    int checkDic();
    void Print();
private:
    ghttp_request * req;
    int request(std::string url);
    std::vector<std::string> targets;
    std::string urlists;
    std::string currentUrl;
    std::vector<struct requestResult> httpresult;

    // void debug();
};
#endif //UNTITLED1_HTTP_H

http.cpp :

//
// Created by payloads on 11/19/17.
//
#include "http.h"
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <regex>
http::http(std::string urlists){
    this->urlists = urlists;
    req = ghttp_request_new();
    ghttp_set_type(req,ghttp_type_get);
    ghttp_set_sync(req,ghttp_sync);
}
int http::checkDic() {
    std::fstream URLio(urlists,std::ios::in);
    std::string buff;
    if(URLio.is_open()){
        while(!URLio.eof()){
            getline(URLio,buff);
            targets.push_back(buff);
        }
    }else{
        return -1;
    }
    return 0;
}
void http::getUrlList() {
    for(auto it=targets.begin();it != targets.end(); it ++){
        std::cout << *it << std::endl;
        currentUrl = *it;
        request(*it);
        std::regex title("<title>([^<]+)</title>");
        std::smatch match_title;
        struct requestResult * res = new requestResult;
        res->status_code = ghttp_status_code(req);
        res->server = ghttp_get_header(req,"Server")==NULL?"":ghttp_get_header(req,"Server");
        res->power = ghttp_get_header(req,"X-Powered-By") == NULL?"":ghttp_get_header(req,"X-Powered-By");
        std::string body = ghttp_get_body(req)==NULL?"":ghttp_get_body(req);
        res->title = regex_search(body, match_title, title)?match_title[1].str():"";
        res->url = currentUrl;
        httpresult.push_back(*res);
        delete res;
    }
}
int http::request(std::string url) {
    ghttp_set_uri(req,(char *)url.data());
    ghttp_prepare(req);
    ghttp_process(req);
}
http::~http() {
    ghttp_flush_response_buffer(req);
    ghttp_request_destroy(req);
}
void http::Print() {
    for(auto it = httpresult.begin();it != httpresult.end(); it++){
        std::cout <<" URL -> "  << it->url << " Title -> " << it->title
                  << std::endl << " Server -> " << it->server
                  << std::endl << " Power -> " << it->power
                  << std::endl << " Status_Code -> " << it->status_code
                  << std::endl;
    }
}

0x04 参考文档

https://sourcecodebrowser.com/libghttp/1.0.9/dir_506518e21dfd39d0ce0207889f9217e3.html


文章来源: https://payloads.online/archivers/2017-11-19/1
如有侵权请联系:admin#unsafe.sh