|
6 | 6 | #include "acl_cpp/session/session.hpp"
|
7 | 7 | #include "acl_cpp/stream/socket_stream.hpp"
|
8 | 8 | #include "acl_cpp/stdlib/charset_conv.hpp"
|
| 9 | +#include "acl_cpp/stdlib/xml.hpp" |
| 10 | +#include "acl_cpp/stdlib/json.hpp" |
9 | 11 | #include "acl_cpp/http/http_header.hpp"
|
10 | 12 | #include "acl_cpp/http/HttpCookie.hpp"
|
11 | 13 | #include "acl_cpp/http/http_client.hpp"
|
@@ -34,6 +36,8 @@ HttpServletRequest::HttpServletRequest(HttpServletResponse& res, session& store,
|
34 | 36 | , method_(HTTP_METHOD_UNKNOWN)
|
35 | 37 | , request_type_(HTTP_REQUEST_NORMAL)
|
36 | 38 | , mime_(NULL)
|
| 39 | +, json_(NULL) |
| 40 | +, xml_(NULL) |
37 | 41 | , readHeaderCalled_(false)
|
38 | 42 | {
|
39 | 43 | ACL_SAFE_STRNCPY(cookie_name_, "ACL_SESSION_ID", sizeof(cookie_name_));
|
@@ -64,6 +68,8 @@ HttpServletRequest::~HttpServletRequest(void)
|
64 | 68 | acl_myfree(*it1);
|
65 | 69 | }
|
66 | 70 | delete mime_;
|
| 71 | + delete json_; |
| 72 | + delete xml_; |
67 | 73 | delete http_session_;
|
68 | 74 | }
|
69 | 75 |
|
@@ -418,6 +424,16 @@ http_mime* HttpServletRequest::getHttpMime(void) const
|
418 | 424 | return mime_;
|
419 | 425 | }
|
420 | 426 |
|
| 427 | +json* HttpServletRequest::getJson(void) const |
| 428 | +{ |
| 429 | + return json_; |
| 430 | +} |
| 431 | + |
| 432 | +xml* HttpServletRequest::getXml(void) const |
| 433 | +{ |
| 434 | + return xml_; |
| 435 | +} |
| 436 | + |
421 | 437 | http_request_t HttpServletRequest::getRequestType(void) const
|
422 | 438 | {
|
423 | 439 | return request_type_;
|
@@ -551,67 +567,126 @@ bool HttpServletRequest::readHeader(void)
|
551 | 567 | if (ptr && *ptr)
|
552 | 568 | parseParameters(ptr);
|
553 | 569 |
|
554 |
| - if (method_ == HTTP_METHOD_GET) |
| 570 | + if (method_ != HTTP_METHOD_POST) |
| 571 | + { |
555 | 572 | 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) |
557 | 580 | {
|
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) |
563 | 602 | 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_); |
565 | 607 | }
|
566 | 608 |
|
567 |
| - const char* ctype = getContentType(); |
568 |
| - const char* stype = content_type_.get_stype(); |
| 609 | + return true; |
| 610 | + } |
569 | 611 |
|
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) |
575 | 637 | {
|
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); |
584 | 640 | }
|
| 641 | + acl_myfree(query); |
| 642 | + return ret == -1 ? false : true; |
| 643 | + } |
585 | 644 |
|
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) |
589 | 654 | {
|
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; |
591 | 663 | }
|
| 664 | + return true; |
| 665 | + } |
592 | 666 |
|
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) |
595 | 676 | {
|
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) |
597 | 680 | return false;
|
598 | 681 |
|
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; |
610 | 685 | }
|
611 |
| - else |
612 |
| - request_type_ = HTTP_REQUEST_OTHER; |
| 686 | + return true; |
613 | 687 | }
|
614 | 688 |
|
| 689 | + request_type_ = HTTP_REQUEST_OTHER; |
615 | 690 | return true;
|
616 | 691 | }
|
617 | 692 |
|
|
0 commit comments