添加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头中特定字段
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
*/

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 == 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_ */

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_
#include "common.h"
#include "httpdownloader.h"
#include "httpclient.h"
#include "media.h"
#include "proxyplayer.h"
#include "recorder.h"