Develop and Download Open Source Software

Browse Subversion Repository

Contents of /http/HttpAccess.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 353 - (show annotations) (download) (as text)
Sun Apr 13 11:50:10 2008 UTC (16 years ago) by satofumi
File MIME type: text/x-c++src
File size: 2509 byte(s)
adjut read byte
1 /*!
2 \file
3 \brief HTTP アクセス
4
5 \author Satofumi KAMIMURA
6
7 $Id$
8 */
9
10 #include "HttpAccess.h"
11 #include "NetInit.h"
12 #include "TcpipCtrl.h"
13 #include <string>
14
15 using namespace beego;
16
17
18 struct HttpAccess::pImpl : private NetInit {
19 std::string error_message_;
20 TcpipCtrl con_;
21 std::string server_;
22
23 pImpl(const char* server, long port)
24 : error_message_("no error."), server_(server) {
25
26 if (! con_.connect(server, 80)) {
27 // !!! エラーを LogManager 出力すべき
28 //fprintf(stderr, "HttpAccess::pImpl::connect: %s\n", con_.what());
29
30 error_message_ = con_.what();
31
32 // !!! ここは、できれば例外を投げるべき
33 // !!! 無効なオブジェクトの生成を許可すべきではない
34 return;
35 }
36 }
37
38 bool isLF(char ch) {
39 return ((ch == '\n') || (ch == '\r')) ? true : false;
40 }
41 };
42
43
44 HttpAccess::HttpAccess(const char* server, long port)
45 : pimpl(new pImpl(server, port)) {
46 }
47
48
49 HttpAccess::~HttpAccess(void) {
50 }
51
52
53 const char* HttpAccess::what(void) {
54 return pimpl->error_message_.c_str();
55 }
56
57
58 bool HttpAccess::get(std::vector<char>& page, const char* address) {
59 if (! pimpl->con_.isConnected()) {
60 return false;
61 }
62
63 // !!! CR, LF を文字コードで指定すべき
64 // !!! \n の解釈が、OS によって異なるため
65 std::string request =
66 "GET " + std::string(address) + " HTTP/1.0\r\n"
67 "Host: " + pimpl->server_ + "\r\n"
68 "\r\n";
69
70 //fprintf(stderr, "request: %s\n", request.c_str());
71 pimpl->con_.send(request.c_str(), static_cast<int>(request.size()));
72
73 // !!! えっと、書き直そうね?
74 // !!! ループからして、ひどいよ?
75 int lf_count = 0;
76 char ch;
77 int n;
78 bool now_header = true;
79 std::string line;
80 size_t left_byte = 0; // 残り受信バイト数
81
82 do {
83 if (now_header) {
84 n = pimpl->con_.recv(&ch, 1, 1000);
85 } else {
86 // !!! バッファに読み出し、page に対して格納すべき
87 n = pimpl->con_.recv(&ch, (left_byte > 0 ? 1 : 0), 1000);
88 --left_byte;
89 }
90 if (n > 0) {
91
92 if (! now_header) {
93 page.push_back(ch);
94 } else {
95 // 行を格納し、Content-Length: の数値だけ読み出す
96 if (isprint(ch)) {
97 line.push_back(ch);
98 } else {
99 if (! line.compare(0, 16, "Content-Length: ")) {
100 left_byte = atoi(&line[16]);
101 }
102 line.clear();
103 }
104 }
105
106 if (pimpl->isLF(ch)) {
107 ++lf_count;
108 if (lf_count >= 4) {
109 now_header = false;
110 }
111 } else {
112 lf_count = 0;
113 }
114 }
115 } while (n > 0);
116
117 return true;
118 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26