添加HttpRequester的C接口

This commit is contained in:
xiongziliang 2019-12-25 14:25:26 +08:00
parent 37858f3b51
commit 31f5d77880
10 changed files with 442 additions and 145 deletions

View File

@ -73,7 +73,7 @@ API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx);
//Parser::getValues()["key"],获取HTTP头中特定字段 //Parser::getValues()["key"],获取HTTP头中特定字段
API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key); API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key);
//Parser::Content(),获取HTTP body //Parser::Content(),获取HTTP body
API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx); API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, int *length);
///////////////////////////////////////////MediaInfo///////////////////////////////////////////// ///////////////////////////////////////////MediaInfo/////////////////////////////////////////////
//MediaInfo对象的C映射 //MediaInfo对象的C映射
@ -137,23 +137,62 @@ API_EXPORT uint16_t API_CALL mk_tcp_session_peer_port(const mk_tcp_session ctx);
//TcpSession::get_local_port() //TcpSession::get_local_port()
API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx); API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx);
///////////////////////////////////////////HttpBody/////////////////////////////////////////////
//HttpBody对象的C映射
typedef void* mk_http_body;
/**
* HttpStringBody
* @param str
* @param len 0strlen获取
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_string(const char *str,int len);
/**
* HttpFileBody
* @param file_path
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_file(const char *file_path);
/**
* HttpMultiFormBody
* @param key_val key-value
* @param file_path
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_multi_form(const char *key_val[],const char *file_path);
/**
* HttpBody
*/
API_EXPORT void API_CALL mk_http_body_release(mk_http_body ctx);
///////////////////////////////////////////HttpResponseInvoker///////////////////////////////////////////// ///////////////////////////////////////////HttpResponseInvoker/////////////////////////////////////////////
//HttpSession::HttpResponseInvoker对象的C映射 //HttpSession::HttpResponseInvoker对象的C映射
typedef void* mk_http_response_invoker; typedef void* mk_http_response_invoker;
/**
* HttpSession::HttpResponseInvoker(const string &codeOut, const StrCaseMap &headerOut, const HttpBody::Ptr &body);
* @param response_code 200 OK
* @param response_header http头 {"Content-Type","text/html",NULL} NULL结尾
* @param response_body body对象
*/
API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx,
const char *response_code,
const char **response_header,
const mk_http_body response_body);
/** /**
* HttpSession::HttpResponseInvoker(const string &codeOut, const StrCaseMap &headerOut, const string &body); * HttpSession::HttpResponseInvoker(const string &codeOut, const StrCaseMap &headerOut, const string &body);
* @param response_code 200 OK * @param response_code 200 OK
* @param response_header http头 {"Content-Type","text/html",NULL} NULL结尾 * @param response_header http头 {"Content-Type","text/html",NULL} NULL结尾
* @param response_content content部分 * @param response_content content部分
*/ */
API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx, API_EXPORT void API_CALL mk_http_response_invoker_do_string(const mk_http_response_invoker ctx,
const char *response_code, const char *response_code,
const char *response_header[], const char **response_header,
const char *response_content); const char *response_content);
/** /**
* HttpSession::HttpResponseInvoker(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const string &filePath); * HttpSession::HttpResponseInvoker(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const string &filePath);
* @param request_parser mk_parser对象http头中的Range字段 * @param request_parser mk_parser对象http头中的Range字段fseek然后再发送文件部分片段
* @param response_header http头 {"Content-Type","text/html",NULL} NULL结尾 * @param response_header http头 {"Content-Type","text/html",NULL} NULL结尾
* @param response_file_path content部分/path/to/html/file * @param response_file_path content部分/path/to/html/file
*/ */

174
api/include/httpclient.h Executable file
View File

@ -0,0 +1,174 @@
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MK_HTTPCLIENT_H_
#define MK_HTTPCLIENT_H_
#include "common.h"
#include "events_objects.h"
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////HttpDownloader/////////////////////////////////////////////
typedef void *mk_http_downloader;
/**
* @param user_data
* @param code 0
* @param err_msg
* @param file_path
*/
typedef void(API_CALL *on_mk_download_complete)(void *user_data, int code, const char *err_msg, const char *file_path);
/**
* http[s]
* @return
*/
API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create();
/**
* http[s]
* @param ctx
*/
API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx);
/**
* http[s]
* @param ctx
* @param url http[s]url
* @param file
* @param cb
* @param user_data
*/
API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data);
///////////////////////////////////////////HttpRequester/////////////////////////////////////////////
typedef void *mk_http_requester;
/**
* http请求结果回调
* code == 0http会话是完整的http回复
* user_data获取到mk_http_requester对象
* mk_http_requester_get_response等函数获取相关回复数据
* mk_http_requester_release函数销毁该对象
* mk_http_requester_clear函数后再复用该对象
* @param user_data
* @param code 0
* @param err_msg
*/
typedef void(API_CALL *on_mk_http_requester_complete)(void *user_data, int code, const char *err_msg);
/**
* HttpRequester
*/
API_EXPORT mk_http_requester API_CALL mk_http_requester_create();
/**
* mk_http_requester对象时才需要用到此方法
*/
API_EXPORT void API_CALL mk_http_requester_clear(mk_http_requester ctx);
/**
* HttpRequester
* mk_http_requester_start函数且正在等待http回复
* mk_http_requester_release方法取消本次http请求
*/
API_EXPORT void API_CALL mk_http_requester_release(mk_http_requester ctx);
/**
* HTTP方法GET/POST
*/
API_EXPORT void API_CALL mk_http_requester_set_method(mk_http_requester ctx,const char *method);
/**
* HTTP头
* @param header {"Content-Type","text/html",NULL} NULL结尾
*/
API_EXPORT void API_CALL mk_http_requester_set_header(mk_http_requester ctx, const char *header[]);
/**
* HTTP头
* @param key Content-Type
* @param value text/html
* @param force key
*/
API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,const char *key,const char *value,int force);
/**
*
* @param body mk_http_body对象mk_http_body_from_string等函数生成使mk_http_body_release释放之
*/
API_EXPORT void API_CALL mk_http_requester_set_body(mk_http_requester ctx, mk_http_body body);
/**
* HTTP回复后可调用该方法获取状态码
* @return 200 OK
*/
API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx);
/**
* HTTP回复后可调用该方法获取响应HTTP头
* @param key HTTP头键名
* @return HTTP头键值
*/
API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key);
/**
* HTTP回复后可调用该方法获取响应HTTP body
* @param length body长度,null
* @return body指针
*/
API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requester ctx, int *length);
/**
* HTTP回复后可调用该方法获取响应
* @return
*/
API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx);
/**
*
* @param cb
* @param user_data
*/
API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data);
/**
* url请求
* @param url urlhttp/https
* @param timeout_second
*/
API_EXPORT void API_CALL mk_http_requester_start(mk_http_requester ctx,const char *url, float timeout_second);
#ifdef __cplusplus
}
#endif
#endif /* MK_HTTPCLIENT_H_ */

View File

@ -1,72 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MK_HTTP_DOWNLOADER_H_
#define MK_HTTP_DOWNLOADER_H_
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void *mk_http_downloader;
/**
* @param user_data
* @param code 0
* @param err_msg
* @param file_path
*/
typedef void(API_CALL *on_mk_download_complete)(void *user_data, int code, const char *err_msg, const char *file_path);
/**
* http[s]
* @return
*/
API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create();
/**
* http[s]
* @param ctx
*/
API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx);
/**
* http[s]
* @param ctx
* @param url http[s]url
* @param file
* @param cb
* @param user_data
*/
API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data);
#ifdef __cplusplus
}
#endif
#endif /* MK_HTTP_DOWNLOADER_H_ */

View File

@ -28,7 +28,7 @@
#define MK_API_H_ #define MK_API_H_
#include "common.h" #include "common.h"
#include "httpdownloader.h" #include "httpclient.h"
#include "media.h" #include "media.h"
#include "proxyplayer.h" #include "proxyplayer.h"
#include "recorder.h" #include "recorder.h"

View File

@ -30,6 +30,8 @@
#include "Record/MP4Recorder.h" #include "Record/MP4Recorder.h"
#include "Network/TcpSession.h" #include "Network/TcpSession.h"
#include "Http/HttpSession.h" #include "Http/HttpSession.h"
#include "Http/HttpBody.h"
#include "Http/HttpClient.h"
#include "Rtsp/RtspSession.h" #include "Rtsp/RtspSession.h"
using namespace mediakit; using namespace mediakit;
@ -130,9 +132,12 @@ API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const c
Parser *parser = (Parser *)ctx; Parser *parser = (Parser *)ctx;
return parser->getValues()[key].c_str(); return parser->getValues()[key].c_str();
} }
API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx){ API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, int *length){
assert(ctx); assert(ctx);
Parser *parser = (Parser *)ctx; Parser *parser = (Parser *)ctx;
if(length){
*length = parser->Content().size();
}
return parser->Content().c_str(); return parser->Content().c_str();
} }
@ -250,9 +255,23 @@ API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx)
return session->get_local_port(); return session->get_local_port();
} }
///////////////////////////////////////////HttpResponseInvoker///////////////////////////////////////////// ///////////////////////////////////////////HttpBody/////////////////////////////////////////////
static StrCaseMap get_http_header( const char *response_header[]){ API_EXPORT mk_http_body API_CALL mk_http_body_from_string(const char *str,int len){
StrCaseMap header; assert(str);
if(!len){
len = strlen(str);
}
return new HttpBody::Ptr(new HttpStringBody(string(str,len)));
}
API_EXPORT mk_http_body API_CALL mk_http_body_from_file(const char *file_path){
assert(file_path);
return new HttpBody::Ptr(new HttpFileBody(file_path));
}
template <typename C = StrCaseMap>
static C get_http_header( const char *response_header[]){
C header;
for (int i = 0; response_header[i] != NULL;) { for (int i = 0; response_header[i] != NULL;) {
auto key = response_header[i]; auto key = response_header[i];
auto value = response_header[i + 1]; auto value = response_header[i + 1];
@ -265,10 +284,23 @@ static StrCaseMap get_http_header( const char *response_header[]){
} }
return std::move(header); return std::move(header);
} }
API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx,
const char *response_code, API_EXPORT mk_http_body API_CALL mk_http_body_from_multi_form(const char *key_val[],const char *file_path){
const char *response_header[], assert(key_val && file_path);
const char *response_content){ return new HttpBody::Ptr(new HttpMultiFormBody(get_http_header<HttpArgs>(key_val),file_path));
}
API_EXPORT void API_CALL mk_http_body_release(mk_http_body ctx){
assert(ctx);
HttpBody::Ptr *ptr = (HttpBody::Ptr *)ctx;
delete ptr;
}
///////////////////////////////////////////HttpResponseInvoker/////////////////////////////////////////////
API_EXPORT void API_CALL mk_http_response_invoker_do_string(const mk_http_response_invoker ctx,
const char *response_code,
const char **response_header,
const char *response_content){
assert(ctx && response_code && response_header && response_content); assert(ctx && response_code && response_header && response_content);
auto header = get_http_header(response_header); auto header = get_http_header(response_header);
HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx; HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
@ -285,6 +317,17 @@ API_EXPORT void API_CALL mk_http_response_invoker_do_file(const mk_http_response
(*invoker).responseFile(((Parser*)(request_parser))->getValues(),header,response_file_path); (*invoker).responseFile(((Parser*)(request_parser))->getValues(),header,response_file_path);
} }
API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx,
const char *response_code,
const char **response_header,
const mk_http_body response_body){
assert(ctx && response_code && response_header && response_body);
auto header = get_http_header(response_header);
HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
HttpBody::Ptr *body = (HttpBody::Ptr*) response_body;
(*invoker)(response_code,header,*body);
}
API_EXPORT mk_http_response_invoker API_CALL mk_http_response_invoker_clone(const mk_http_response_invoker ctx){ API_EXPORT mk_http_response_invoker API_CALL mk_http_response_invoker_clone(const mk_http_response_invoker ctx){
assert(ctx); assert(ctx);
HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx; HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;

159
api/source/httpclient.cpp Executable file
View File

@ -0,0 +1,159 @@
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "httpclient.h"
#include "Util/logger.h"
#include "Http/HttpDownloader.h"
#include "Http/HttpRequester.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create() {
HttpDownloader::Ptr *obj(new HttpDownloader::Ptr(new HttpDownloader()));
return (mk_http_downloader) obj;
}
API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx) {
assert(ctx);
HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx;
delete obj;
}
API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data) {
assert(ctx && url && file);
HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx;
(*obj)->setOnResult([cb, user_data](ErrCode code, const string &errMsg, const string &filePath) {
if (cb) {
cb(user_data, code, errMsg.data(), filePath.data());
}
});
(*obj)->startDownload(url, file, false);
}
///////////////////////////////////////////HttpRequester/////////////////////////////////////////////
API_EXPORT mk_http_requester API_CALL mk_http_requester_create(){
HttpRequester::Ptr *ret = new HttpRequester::Ptr(new HttpRequester);
return ret;
}
API_EXPORT void API_CALL mk_http_requester_clear(mk_http_requester ctx){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
(*obj)->clear();
}
API_EXPORT void API_CALL mk_http_requester_release(mk_http_requester ctx){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
delete obj;
}
API_EXPORT void API_CALL mk_http_requester_set_method(mk_http_requester ctx,const char *method){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
(*obj)->setMethod(method);
}
template <typename C = StrCaseMap>
static C get_http_header( const char *response_header[]){
C header;
for (int i = 0; response_header[i] != NULL;) {
auto key = response_header[i];
auto value = response_header[i + 1];
if (key && value) {
i += 2;
header[key] = value;
continue;
}
break;
}
return std::move(header);
}
API_EXPORT void API_CALL mk_http_requester_set_body(mk_http_requester ctx, mk_http_body body){
assert(ctx && body);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
HttpBody::Ptr *body_obj = (HttpBody::Ptr *)body;
(*obj)->setBody(*body_obj);
}
API_EXPORT void API_CALL mk_http_requester_set_header(mk_http_requester ctx, const char *header[]){
assert(ctx && header);
auto header_obj = get_http_header(header);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
(*obj)->setHeader(header_obj);
}
API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,const char *key,const char *value,int force){
assert(ctx && key && value);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
(*obj)->addHeader(key,value,force);
}
API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
return (*obj)->responseStatus().c_str();
}
API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
return (*obj)->response()[key].c_str();
}
API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requester ctx, int *length){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
if(length){
*length = (*obj)->response().Content().size();
}
return (*obj)->response().Content().c_str();
}
API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx){
assert(ctx);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
return (mk_parser)&((*obj)->response());
}
API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data){
assert(ctx && cb);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
(*obj)->setOnResult([cb,user_data](const SockException &ex,const string &status,const StrCaseMap &header,const string &strRecvBody){
cb(user_data, ex.getErrCode(),ex.what());
});
}
API_EXPORT void API_CALL mk_http_requester_start(mk_http_requester ctx,const char *url, float timeout_second){
assert(ctx && url && url[0] && timeout_second > 0);
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
(*obj)->sendRequest(url,timeout_second);
}

View File

@ -1,54 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "httpdownloader.h"
#include "Util/logger.h"
#include "Http/HttpDownloader.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create() {
HttpDownloader::Ptr *obj(new HttpDownloader::Ptr(new HttpDownloader()));
return (mk_http_downloader) obj;
}
API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx) {
assert(ctx);
HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx;
delete obj;
}
API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data) {
assert(ctx && url && file);
HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx;
(*obj)->setOnResult([cb, user_data](ErrCode code, const string &errMsg, const string &filePath) {
if (cb) {
cb(user_data, code, errMsg.data(), filePath.data());
}
});
(*obj)->startDownload(url, file, false);
}

View File

@ -169,7 +169,7 @@ void API_CALL on_mk_http_request(const mk_parser parser,
mk_parser_get_url_params(parser), mk_parser_get_url_params(parser),
mk_parser_get_tail(parser), mk_parser_get_tail(parser),
mk_parser_get_header(parser, "User-Agent"), mk_parser_get_header(parser, "User-Agent"),
mk_parser_get_content(parser)); mk_parser_get_content(parser,NULL));
const char *url = mk_parser_get_url(parser); const char *url = mk_parser_get_url(parser);
if(strcmp(url,"/api/test") != 0){ if(strcmp(url,"/api/test") != 0){
@ -190,7 +190,9 @@ void API_CALL on_mk_http_request(const mk_parser parser,
"<center>""ZLMediaKit-4.0</center>" "<center>""ZLMediaKit-4.0</center>"
"</body>" "</body>"
"</html>"; "</html>";
mk_http_response_invoker_do(invoker,"200 OK",response_header,content); mk_http_body body = mk_http_body_from_string(content,0);
mk_http_response_invoker_do(invoker, "200 OK", response_header, body);
mk_http_body_release(body);
} }
/** /**
@ -224,7 +226,7 @@ void API_CALL on_mk_http_access(const mk_parser parser,
mk_parser_get_url_params(parser), mk_parser_get_url_params(parser),
mk_parser_get_tail(parser), mk_parser_get_tail(parser),
mk_parser_get_header(parser,"User-Agent"), mk_parser_get_header(parser,"User-Agent"),
mk_parser_get_content(parser)); mk_parser_get_content(parser,NULL));
//有访问权限,每次访问文件都需要鉴权 //有访问权限,每次访问文件都需要鉴权
mk_http_access_path_invoker_do(invoker, NULL, NULL, 0); mk_http_access_path_invoker_do(invoker, NULL, NULL, 0);
@ -257,7 +259,7 @@ void API_CALL on_mk_http_before_access(const mk_parser parser,
mk_parser_get_url_params(parser), mk_parser_get_url_params(parser),
mk_parser_get_tail(parser), mk_parser_get_tail(parser),
mk_parser_get_header(parser, "User-Agent"), mk_parser_get_header(parser, "User-Agent"),
mk_parser_get_content(parser)); mk_parser_get_content(parser,NULL));
//覆盖path的值可以重定向文件 //覆盖path的值可以重定向文件
} }

View File

@ -53,6 +53,7 @@ void HttpRequester::onResponseCompleted() {
void HttpRequester::onDisconnect(const SockException &ex){ void HttpRequester::onDisconnect(const SockException &ex){
if(_onResult){ if(_onResult){
const_cast<Parser &>(response()).setContent(_strRecvBody);
_onResult(ex,responseStatus(),responseHeader(),_strRecvBody); _onResult(ex,responseStatus(),responseHeader(),_strRecvBody);
_onResult = nullptr; _onResult = nullptr;
} }
@ -69,5 +70,9 @@ void HttpRequester::clear() {
_onResult = nullptr; _onResult = nullptr;
} }
void HttpRequester::setOnResult(const HttpRequesterResult &onResult) {
_onResult = onResult;
}
}//namespace mediakit }//namespace mediakit

View File

@ -38,6 +38,7 @@ public:
typedef std::function<void(const SockException &ex,const string &status,const HttpHeader &header,const string &strRecvBody)> HttpRequesterResult; typedef std::function<void(const SockException &ex,const string &status,const HttpHeader &header,const string &strRecvBody)> HttpRequesterResult;
HttpRequester(); HttpRequester();
virtual ~HttpRequester(); virtual ~HttpRequester();
void setOnResult(const HttpRequesterResult &onResult);
void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10); void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10);
void clear() override ; void clear() override ;
private: private: