2024-08-13 17:07:34 +08:00
|
|
|
#ifndef CCWeb_Request_H
|
|
|
|
|
#define CCWeb_Request_H
|
|
|
|
|
#pragma once
|
|
|
|
|
#include "CCString.h"
|
|
|
|
|
#include "map"
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include "CORS.h"
|
|
|
|
|
#include "../../include/CCJSONObject.h"
|
|
|
|
|
#include "../../include/CCFile.h"
|
|
|
|
|
#include "../../include/CCSocket.h"
|
2024-11-01 16:09:31 +08:00
|
|
|
#include "../../include/CCThread.h"
|
|
|
|
|
#include "../../include/CCThreadPool.h"
|
|
|
|
|
#include "CCObject.h"
|
|
|
|
|
#include "../../include/CCFileOutStream.h"
|
|
|
|
|
#define Buffer_Max 1024
|
2024-08-13 17:07:34 +08:00
|
|
|
|
2024-11-01 16:09:31 +08:00
|
|
|
class CCRequest;
|
2024-08-13 17:07:34 +08:00
|
|
|
enum RequestProcess
|
|
|
|
|
{
|
|
|
|
|
RequestProcess_First,
|
|
|
|
|
RequestProcess_Header,
|
|
|
|
|
RequestProcess_Body,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct BufferReader
|
|
|
|
|
{
|
|
|
|
|
CCString Body;
|
|
|
|
|
CCString Method;
|
|
|
|
|
CCString Path;
|
|
|
|
|
CCString Version;
|
2024-11-01 16:09:31 +08:00
|
|
|
std::vector<char> Buffer;
|
2024-08-13 17:07:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct BufferFile
|
|
|
|
|
{
|
|
|
|
|
BufferFile() = default;
|
|
|
|
|
std::vector<char> Buffer;
|
|
|
|
|
std::map<CCString,CCString> Headers;
|
|
|
|
|
CCString GetFileName();
|
|
|
|
|
bool Save(const CCString& Path);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum MethodType
|
|
|
|
|
{
|
|
|
|
|
MethodType_GET,
|
|
|
|
|
MethodType_POST,
|
|
|
|
|
MethodType_PUT,
|
|
|
|
|
MethodType_DELETE,
|
|
|
|
|
MethodType_HEAD,
|
|
|
|
|
MethodType_OPTIONS,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class OutPutStream
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
CCString RMethod = "GET";
|
|
|
|
|
CCString RPath,RBody;
|
|
|
|
|
std::map<CCString,CCString> SendHeaders = {{"Method",RMethod}};
|
|
|
|
|
CCSocket Socket;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
OutPutStream(CCSocket& sc);
|
|
|
|
|
void SetMethod(MethodType method);
|
|
|
|
|
void AddHeader(const CCString& Key,const CCString& Value);
|
|
|
|
|
void SetHost(const CCString& host);
|
|
|
|
|
void SetBufferBody(CCString body);
|
|
|
|
|
void Send();
|
2024-11-01 16:09:31 +08:00
|
|
|
CCSocket GetSocket();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class InPutStream:public CCFileOutStream
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<char> Buffer;
|
|
|
|
|
BufferReader bufferReader;
|
|
|
|
|
CCSocket Socket;
|
|
|
|
|
std::map<CCString,CCString> FileHeaders;
|
|
|
|
|
std::map<CCString,CCString> Headers;
|
|
|
|
|
public:
|
|
|
|
|
InPutStream() = default;
|
|
|
|
|
InPutStream(CCRequest* Res);
|
|
|
|
|
void WriteBufferToFile(const CCString& Path);
|
|
|
|
|
CCObject Get(CCString key);
|
|
|
|
|
private:
|
|
|
|
|
void DeleteTailOf(CCString Path,CCString str);
|
|
|
|
|
std::string readTailOfFile(const std::string& filepath, size_t bufferSize);
|
|
|
|
|
bool filterAndPrint(const std::string& tail, const std::string& filter);
|
2024-08-13 17:07:34 +08:00
|
|
|
};
|
|
|
|
|
|
2024-11-01 16:09:31 +08:00
|
|
|
|
2024-08-13 17:07:34 +08:00
|
|
|
class CCRequest
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
BufferReader Buffer;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
std::map<CCString,CCString> Headers;
|
2024-11-01 16:09:31 +08:00
|
|
|
std::map<CCString,CCString> FileHeaders;
|
2024-08-13 17:07:34 +08:00
|
|
|
public:
|
|
|
|
|
CCRequest() = default;
|
|
|
|
|
CCRequest(CCSocket& sc);
|
|
|
|
|
CCString GetFileSuffix(const CCString& filePath);
|
|
|
|
|
CCString GetFileType(const CCString& str);
|
|
|
|
|
unsigned int GetFileSize(const char* path);
|
|
|
|
|
void SetBuffer(BufferReader& buffer);
|
|
|
|
|
BufferReader GetReader();
|
2024-11-01 16:09:31 +08:00
|
|
|
static JSON GetJson(const BufferReader& instr);
|
2024-08-13 17:07:34 +08:00
|
|
|
BufferFile GetFile(BufferReader in);
|
|
|
|
|
CCString GetParameter(CCString string);
|
2024-11-01 16:09:31 +08:00
|
|
|
BufferReader ReaderFileData();
|
|
|
|
|
OutPutStream GetWriter();
|
2024-08-13 17:07:34 +08:00
|
|
|
void GetFormData(CCString& input,CCString& key,CCString& value);
|
|
|
|
|
CCString GetFileDateHeader(BufferReader in,std::map<CCString,CCString>& map);
|
2024-11-01 16:09:31 +08:00
|
|
|
CCString GetFileDateHeader(CCString& in,std::map<CCString,CCString>& map);
|
2024-08-13 17:07:34 +08:00
|
|
|
std::vector<char> ReaderFormData(BufferReader in, std::map<CCString, CCString> &map);
|
2024-11-01 16:09:31 +08:00
|
|
|
InPutStream GetInputStream();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::map<std::string, std::string> parseKeyValuePairs(const std::string& input);
|
2024-08-13 17:07:34 +08:00
|
|
|
CCSocket Socket;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|