ZLMediaKit/api/include/mk_httpclient.h

160 lines
5.4 KiB
C++
Raw Permalink Normal View History

2019-12-25 14:25:26 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2019-12-25 14:25:26 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2019-12-25 14:25:26 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2019-12-25 14:25:26 +08:00
*/
#ifndef MK_HTTPCLIENT_H_
#define MK_HTTPCLIENT_H_
#include "mk_common.h"
#include "mk_events_objects.h"
2019-12-25 14:25:26 +08:00
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////HttpDownloader/////////////////////////////////////////////
typedef struct mk_http_downloader_t *mk_http_downloader;
2019-12-25 14:25:26 +08:00
/**
* @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);
API_EXPORT void API_CALL mk_http_downloader_start2(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data, on_user_data_free user_data_free);
2019-12-25 14:25:26 +08:00
///////////////////////////////////////////HttpRequester/////////////////////////////////////////////
typedef struct mk_http_requester_t *mk_http_requester;
2019-12-25 14:25:26 +08:00
/**
* 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);
/**
2019-12-25 14:27:43 +08:00
* HTTP方法GET/POST
2019-12-25 14:25:26 +08:00
*/
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, size_t *length);
2019-12-25 14:25:26 +08:00
/**
* 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);
API_EXPORT void API_CALL mk_http_requester_set_cb2(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data, on_user_data_free user_data_free);
2019-12-25 14:25:26 +08:00
/**
* 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_ */