82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using System;
|
|
using System.Threading;
|
|
|
|
namespace UdpClientLib
|
|
{
|
|
public enum HttpMethod
|
|
{
|
|
GET,
|
|
POST,
|
|
PUT,
|
|
DELETE,
|
|
PATCH
|
|
}
|
|
|
|
public class HttpRequest
|
|
{
|
|
public HttpMethod Method { get; set; }
|
|
public string Path { get; set; }
|
|
public Dictionary<string, string> Headers { get; set; }
|
|
public string Body { get; set; }
|
|
public CancellationToken Token { get; set; }
|
|
|
|
// 생성자
|
|
public HttpRequest(HttpMethod method, string path, Dictionary<string, string> headers = null, string body = null, CancellationToken token = default)
|
|
{
|
|
Method = method;
|
|
Path = path;
|
|
Headers = headers ?? new Dictionary<string, string>();
|
|
Body = body;
|
|
Token = token;
|
|
}
|
|
|
|
// 기본 생성자 (Unity Serialization을 위해)
|
|
public HttpRequest()
|
|
{
|
|
Headers = new Dictionary<string, string>();
|
|
}
|
|
}
|
|
|
|
public static class HttpResponseParser
|
|
{
|
|
public static string ExtractJsonFromHttpResponse(string httpResponse, string type = null)
|
|
{
|
|
if (httpResponse.Contains("HTTP/1.1") || type == "udp")
|
|
{
|
|
if (string.IsNullOrEmpty(httpResponse))
|
|
return string.Empty;
|
|
|
|
// HTTP 헤더와 바디를 구분하는 구분자 찾기 (\r\n\r\n)
|
|
string separator = "\r\n\r\n";
|
|
int separatorIndex = httpResponse.IndexOf(separator);
|
|
|
|
if (separatorIndex == -1)
|
|
{
|
|
// \r\n\r\n이 없으면 \n\n도 시도해보기
|
|
separator = "\n\n";
|
|
separatorIndex = httpResponse.IndexOf(separator);
|
|
}
|
|
|
|
|
|
if (separatorIndex == -1)
|
|
{
|
|
throw new InvalidOperationException("HTTP 헤더와 바디 구분자를 찾을 수 없습니다.");
|
|
}
|
|
|
|
// 구분자 이후의 JSON 부분 추출
|
|
string jsonPart = httpResponse.Substring(separatorIndex + separator.Length);
|
|
|
|
// 앞뒤 공백 제거
|
|
return jsonPart.Trim();
|
|
}
|
|
|
|
else
|
|
{
|
|
return httpResponse;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
} |