diff --git a/api/include/events_objects.h b/api/include/events_objects.h index 9c3b9e42..5c464caf 100644 --- a/api/include/events_objects.h +++ b/api/include/events_objects.h @@ -73,7 +73,7 @@ API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx); //Parser::getValues()["key"],获取HTTP头中特定字段 API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key); //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对象的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() 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 字符串长度,为0则用strlen获取 + */ +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///////////////////////////////////////////// //HttpSession::HttpResponseInvoker对象的C映射 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); * @param response_code 譬如200 OK * @param response_header 返回的http头,譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾 * @param response_content 返回的content部分,譬如一个网页内容 */ -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 char *response_content); +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); /** * 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_file_path 返回的content部分,譬如/path/to/html/file */ diff --git a/api/include/httpclient.h b/api/include/httpclient.h new file mode 100755 index 00000000..06de495b --- /dev/null +++ b/api/include/httpclient.h @@ -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 == 0时代表本次http会话是完整的(收到了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 请求url,支持http/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_ */ diff --git a/api/include/httpdownloader.h b/api/include/httpdownloader.h deleted file mode 100755 index 0e8f85fd..00000000 --- a/api/include/httpdownloader.h +++ /dev/null @@ -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_ */ diff --git a/api/include/mediakit.h b/api/include/mediakit.h index a6f42496..2be7bd67 100755 --- a/api/include/mediakit.h +++ b/api/include/mediakit.h @@ -28,7 +28,7 @@ #define MK_API_H_ #include "common.h" -#include "httpdownloader.h" +#include "httpclient.h" #include "media.h" #include "proxyplayer.h" #include "recorder.h" diff --git a/api/source/events_objects.cpp b/api/source/events_objects.cpp index 0a22566d..4108d57e 100644 --- a/api/source/events_objects.cpp +++ b/api/source/events_objects.cpp @@ -30,6 +30,8 @@ #include "Record/MP4Recorder.h" #include "Network/TcpSession.h" #include "Http/HttpSession.h" +#include "Http/HttpBody.h" +#include "Http/HttpClient.h" #include "Rtsp/RtspSession.h" 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; 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); Parser *parser = (Parser *)ctx; + if(length){ + *length = parser->Content().size(); + } 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(); } -///////////////////////////////////////////HttpResponseInvoker///////////////////////////////////////////// -static StrCaseMap get_http_header( const char *response_header[]){ - StrCaseMap header; +///////////////////////////////////////////HttpBody///////////////////////////////////////////// +API_EXPORT mk_http_body API_CALL mk_http_body_from_string(const char *str,int len){ + 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 +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]; @@ -265,10 +284,23 @@ static StrCaseMap get_http_header( const char *response_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, - const char *response_header[], - const char *response_content){ + +API_EXPORT mk_http_body API_CALL mk_http_body_from_multi_form(const char *key_val[],const char *file_path){ + assert(key_val && file_path); + return new HttpBody::Ptr(new HttpMultiFormBody(get_http_header(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); auto header = get_http_header(response_header); 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); } +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){ assert(ctx); HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx; diff --git a/api/source/httpclient.cpp b/api/source/httpclient.cpp new file mode 100755 index 00000000..01e4c68f --- /dev/null +++ b/api/source/httpclient.cpp @@ -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 +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); +} + diff --git a/api/source/httpdownloader.cpp b/api/source/httpdownloader.cpp deleted file mode 100755 index 4e742924..00000000 --- a/api/source/httpdownloader.cpp +++ /dev/null @@ -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); -} diff --git a/api/tests/server.c b/api/tests/server.c index 8da94261..f29ac98b 100644 --- a/api/tests/server.c +++ b/api/tests/server.c @@ -169,7 +169,7 @@ void API_CALL on_mk_http_request(const mk_parser parser, mk_parser_get_url_params(parser), mk_parser_get_tail(parser), 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); if(strcmp(url,"/api/test") != 0){ @@ -190,7 +190,9 @@ void API_CALL on_mk_http_request(const mk_parser parser, "
""ZLMediaKit-4.0
" "" ""; - 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_tail(parser), 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); @@ -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_tail(parser), mk_parser_get_header(parser, "User-Agent"), - mk_parser_get_content(parser)); + mk_parser_get_content(parser,NULL)); //覆盖path的值可以重定向文件 } diff --git a/src/Http/HttpRequester.cpp b/src/Http/HttpRequester.cpp index b96c7d45..aa9c962e 100644 --- a/src/Http/HttpRequester.cpp +++ b/src/Http/HttpRequester.cpp @@ -53,6 +53,7 @@ void HttpRequester::onResponseCompleted() { void HttpRequester::onDisconnect(const SockException &ex){ if(_onResult){ + const_cast(response()).setContent(_strRecvBody); _onResult(ex,responseStatus(),responseHeader(),_strRecvBody); _onResult = nullptr; } @@ -69,5 +70,9 @@ void HttpRequester::clear() { _onResult = nullptr; } +void HttpRequester::setOnResult(const HttpRequesterResult &onResult) { + _onResult = onResult; +} + }//namespace mediakit diff --git a/src/Http/HttpRequester.h b/src/Http/HttpRequester.h index dc6ecfb3..9de7523a 100644 --- a/src/Http/HttpRequester.h +++ b/src/Http/HttpRequester.h @@ -38,6 +38,7 @@ public: typedef std::function HttpRequesterResult; HttpRequester(); virtual ~HttpRequester(); + void setOnResult(const HttpRequesterResult &onResult); void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10); void clear() override ; private: