Skip to content

Commit c67a7e4

Browse files
committed
HttpServletRequest add getJson&getXml methods
1 parent 3e79879 commit c67a7e4

File tree

6 files changed

+156
-46
lines changed

6 files changed

+156
-46
lines changed

lib_acl_cpp/changes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
�޸���ʷ�б���
22

33
------------------------------------------------------------------------
4+
331) 2015.7.25
5+
331.1) feature: HttpServletRequest ��������ֱ����� xml �� json ����ķ���
6+
47
330) 2015.7.22
58
330.1) bugfix: dbuf_pool �������붯̬�����������Ҫ������������Ϊ˽�з���
69

lib_acl_cpp/include/acl_cpp/http/HttpServlet.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class ACL_CPP_API HttpServlet
4747
/**
4848
* 针对 POST 方法,该方法设置解析数据体的最大长度,如果数据体,该函数必须在 doRun
4949
* 之前调用才有效
50-
* @param length {int} 最大长度限制,如果请求的数据体长度过大,则直接返回 false
50+
* @param length {int} 最大长度限制,如果请求的数据体长度过大,则直接返回
51+
* false,如果该值 <= 0 则内部不限制数据体长度,调用该函数前内部缺省值为 0
5152
* @return {HttpServlet&}
5253
*/
5354
HttpServlet& setParseBodyLimit(int length);

lib_acl_cpp/include/acl_cpp/http/HttpServletRequest.hpp

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ostream;
1212
class socket_stream;
1313
class http_client;
1414
class http_mime;
15+
class json;
16+
class xml;
1517
class session;
1618
class HttpSession;
1719
class HttpCookie;
@@ -221,6 +223,26 @@ class ACL_CPP_API HttpServletRequest
221223
*/
222224
http_mime* getHttpMime(void) const;
223225

226+
/**
227+
* 数据类型为 text/json 格式构造函数中的 body_parse 为 true 时,则内部自动
228+
* 解析数据并创建 json 对象
229+
* @return {json*} 返回解析好的 json 对象,若返回 NULL 则有以下几个原因:
230+
* 1、读数据出错
231+
* 2、非 json 数据格式
232+
* 3、body_parse 在构造函数中设置的为 false
233+
*/
234+
json* getJson(void) const;
235+
236+
/**
237+
* 数据类型为 text/xml 格式构造函数中的 body_parse 为 true 时,则内部自动
238+
* 解析数据并创建 xml 对象
239+
* @return {xml*} 返回解析好的 xml 对象,若返回 NULL 则有以下几个原因:
240+
* 1、读数据出错
241+
* 2、非 xml 数据格式
242+
* 3、body_parse 在构造函数中设置的为 false
243+
*/
244+
xml* getXml(void) const;
245+
224246
/**
225247
* 获得 HTTP 请求数据的类型
226248
* @return {http_request_t},一般对 POST 方法中的上传
@@ -304,6 +326,8 @@ class ACL_CPP_API HttpServletRequest
304326
std::vector<HTTP_PARAM*> params_;
305327
http_request_t request_type_;
306328
http_mime* mime_;
329+
json* json_;
330+
xml* xml_;
307331

308332
bool readHeaderCalled_;
309333
bool readHeader(void);

lib_acl_cpp/include/acl_cpp/http/http_type.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ typedef enum
5151
// Content-Type: application/octet-stream
5252
HTTP_REQUEST_OCTET_STREAM,
5353

54+
// Content-Type: text/xml
55+
HTTP_REQUEST_TEXT_XML,
56+
57+
// Content-Type: text/json
58+
HTTP_REQUEST_TEXT_JSON,
59+
5460
// 其它类型
5561
HTTP_REQUEST_OTHER
5662
} http_request_t;

lib_acl_cpp/src/http/HttpServlet.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ HttpServlet::HttpServlet(void)
1717
local_charset_[0] = 0;
1818
rw_timeout_ = 60;
1919
parse_body_enable_ = true;
20-
parse_body_limit_ = 102400;
20+
parse_body_limit_ = 0;
2121
}
2222

2323
HttpServlet::~HttpServlet(void)
@@ -48,7 +48,8 @@ HttpServlet& HttpServlet::setParseBody(bool on)
4848

4949
HttpServlet& HttpServlet::setParseBodyLimit(int length)
5050
{
51-
parse_body_limit_ = length;
51+
if (length > 0)
52+
parse_body_limit_ = length;
5253
return *this;
5354
}
5455

lib_acl_cpp/src/http/HttpServletRequest.cpp

+118-43
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "acl_cpp/session/session.hpp"
77
#include "acl_cpp/stream/socket_stream.hpp"
88
#include "acl_cpp/stdlib/charset_conv.hpp"
9+
#include "acl_cpp/stdlib/xml.hpp"
10+
#include "acl_cpp/stdlib/json.hpp"
911
#include "acl_cpp/http/http_header.hpp"
1012
#include "acl_cpp/http/HttpCookie.hpp"
1113
#include "acl_cpp/http/http_client.hpp"
@@ -34,6 +36,8 @@ HttpServletRequest::HttpServletRequest(HttpServletResponse& res, session& store,
3436
, method_(HTTP_METHOD_UNKNOWN)
3537
, request_type_(HTTP_REQUEST_NORMAL)
3638
, mime_(NULL)
39+
, json_(NULL)
40+
, xml_(NULL)
3741
, readHeaderCalled_(false)
3842
{
3943
ACL_SAFE_STRNCPY(cookie_name_, "ACL_SESSION_ID", sizeof(cookie_name_));
@@ -64,6 +68,8 @@ HttpServletRequest::~HttpServletRequest(void)
6468
acl_myfree(*it1);
6569
}
6670
delete mime_;
71+
delete json_;
72+
delete xml_;
6773
delete http_session_;
6874
}
6975

@@ -418,6 +424,16 @@ http_mime* HttpServletRequest::getHttpMime(void) const
418424
return mime_;
419425
}
420426

427+
json* HttpServletRequest::getJson(void) const
428+
{
429+
return json_;
430+
}
431+
432+
xml* HttpServletRequest::getXml(void) const
433+
{
434+
return xml_;
435+
}
436+
421437
http_request_t HttpServletRequest::getRequestType(void) const
422438
{
423439
return request_type_;
@@ -551,67 +567,126 @@ bool HttpServletRequest::readHeader(void)
551567
if (ptr && *ptr)
552568
parseParameters(ptr);
553569

554-
if (method_ == HTTP_METHOD_GET)
570+
if (method_ != HTTP_METHOD_POST)
571+
{
555572
request_type_ = HTTP_REQUEST_NORMAL;
556-
else if (method_ == HTTP_METHOD_POST)
573+
return true;
574+
}
575+
576+
acl_int64 len = getContentLength();
577+
if (len < -1)
578+
return false;
579+
if (len == 0)
557580
{
558-
acl_int64 len = getContentLength();
559-
if (len < -1)
560-
return false;
561-
if (len == 0)
562-
{
581+
request_type_ = HTTP_REQUEST_NORMAL;
582+
return true;
583+
}
584+
585+
const char* ctype = getContentType();
586+
const char* stype = content_type_.get_stype();
587+
588+
// 数据体为文件上传的 MIME 类型
589+
if (ctype == NULL || stype == NULL)
590+
{
591+
request_type_ = HTTP_REQUEST_OTHER;
592+
return true;
593+
}
594+
595+
#define EQ !strcasecmp
596+
597+
// 当数据体为 HTTP MIME 上传数据时
598+
if (EQ(ctype, "multipart") && EQ(stype, "form-data"))
599+
{
600+
const char* bound = content_type_.get_bound();
601+
if (bound == NULL)
563602
request_type_ = HTTP_REQUEST_NORMAL;
564-
return true;
603+
else
604+
{
605+
request_type_ = HTTP_REQUEST_MULTIPART_FORM;
606+
mime_ = NEW http_mime(bound, localCharset_);
565607
}
566608

567-
const char* ctype = getContentType();
568-
const char* stype = content_type_.get_stype();
609+
return true;
610+
}
569611

570-
// 数据体为文件上传的 MIME 类型
571-
if (ctype == NULL)
572-
request_type_ = HTTP_REQUEST_OTHER;
573-
else if (strcasecmp(ctype, "multipart") == 0
574-
&& strcasecmp(stype, "form-data") == 0)
612+
// 数据体为数据流类型
613+
if (EQ(ctype, "application") && EQ(stype, "octet-stream"))
614+
{
615+
request_type_ = HTTP_REQUEST_OCTET_STREAM;
616+
return true;
617+
}
618+
619+
// 数据体为普通的 name=value 类型
620+
if (!body_parse_)
621+
{
622+
request_type_ = HTTP_REQUEST_OTHER;
623+
return true;
624+
}
625+
626+
// 如果需要分析数据体的参数时的数据体长度过大,则直接返回错误
627+
if (body_limit_ > 0 && len >= body_limit_)
628+
return false;
629+
630+
// 当数据体为 form 格式时:
631+
if (EQ(ctype, "application") && EQ(stype, "x-www-form-urlencoded"))
632+
{
633+
request_type_ = HTTP_REQUEST_NORMAL;
634+
char* query = (char*) acl_mymalloc((size_t) len + 1);
635+
int ret = getInputStream().read(query, (size_t) len);
636+
if (ret > 0)
575637
{
576-
const char* bound = content_type_.get_bound();
577-
if (bound == NULL)
578-
request_type_ = HTTP_REQUEST_NORMAL;
579-
else
580-
{
581-
request_type_ = HTTP_REQUEST_MULTIPART_FORM;
582-
mime_ = NEW http_mime(bound, localCharset_);
583-
}
638+
query[ret] = 0;
639+
parseParameters(query);
584640
}
641+
acl_myfree(query);
642+
return ret == -1 ? false : true;
643+
}
585644

586-
// 数据体为数据流类型
587-
else if (strcasecmp(ctype, "application") == 0
588-
&& strcasecmp(stype, "octet-stream") == 0)
645+
// 当数据类型为 text/json 格式时:
646+
if (EQ(ctype, "text") && EQ(stype, "json"))
647+
{
648+
request_type_ = HTTP_REQUEST_TEXT_JSON;
649+
json_ = NEW json();
650+
char buf[8192];
651+
acl_int64 n;
652+
istream& in = getInputStream();
653+
while (len > 0)
589654
{
590-
request_type_ = HTTP_REQUEST_OCTET_STREAM;
655+
n = sizeof(buf) - 1 > len ? len : sizeof(buf) - 1;
656+
n = in.read(buf, (size_t) n);
657+
if (n == -1)
658+
return false;
659+
660+
buf[n] = 0;
661+
json_->update(buf);
662+
len -= n;
591663
}
664+
return true;
665+
}
592666

593-
// 数据体为普通的 name=value 类型
594-
else if (body_parse_)
667+
// 当数据类型为 text/xml 格式时:
668+
if (EQ(ctype, "text") && EQ(stype, "xml"))
669+
{
670+
request_type_ = HTTP_REQUEST_TEXT_XML;
671+
xml_ = NEW xml();
672+
char buf[8192];
673+
acl_int64 n;
674+
istream& in = getInputStream();
675+
while (len > 0)
595676
{
596-
if (len >= body_limit_)
677+
n = sizeof(buf) - 1 > len ? len : sizeof(buf) - 1;
678+
n = in.read(buf, (size_t) n);
679+
if (n == -1)
597680
return false;
598681

599-
request_type_ = HTTP_REQUEST_NORMAL;
600-
601-
char* query = (char*) acl_mymalloc((size_t) len + 1);
602-
int ret = getInputStream().read(query, (size_t) len);
603-
if (ret > 0)
604-
{
605-
query[ret] = 0;
606-
parseParameters(query);
607-
}
608-
acl_myfree(query);
609-
return ret == -1 ? false : true;
682+
buf[n] = 0;
683+
xml_->update(buf);
684+
len -= n;
610685
}
611-
else
612-
request_type_ = HTTP_REQUEST_OTHER;
686+
return true;
613687
}
614688

689+
request_type_ = HTTP_REQUEST_OTHER;
615690
return true;
616691
}
617692

0 commit comments

Comments
 (0)