• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂505 (tree)
時間2012-06-23 16:24:51
作者ornse01

Log Message

replace http request implement for bbsmenuretriever.

Change Summary

差異

--- bchanl/trunk/src/bbsmenuretriever.c (revision 504)
+++ bchanl/trunk/src/bbsmenuretriever.c (revision 505)
@@ -1,7 +1,7 @@
11 /*
22 * bbsmenuretriever.c
33 *
4- * Copyright (c) 2009-2010 project bchan
4+ * Copyright (c) 2009-2012 project bchan
55 *
66 * This software is provided 'as-is', without any express or implied
77 * warranty. In no event will the authors be held liable for any damages
@@ -34,8 +34,9 @@
3434
3535 #include "bbsmenuretriever.h"
3636
37-#include "retriever.h"
3837 #include "bbsmenucache.h"
38+#include <http/http_typedef.h>
39+#include <http/http_connector.h>
3940
4041 #ifdef BCHANL_CONFIG_DEBUG
4142 # define DP(arg) printf arg
@@ -46,10 +47,12 @@
4647 #endif
4748
4849 struct bbsmnretriever_t_ {
49- retriever_t *retr;
50+ http_connector_t *connector;
51+ ID endpoint;
52+ HTTP_STATUSCODE status;
5053 };
5154
52-EXPORT bbsmnretriever_t* bbsmnretriever_new()
55+EXPORT bbsmnretriever_t* bbsmnretriever_new(http_connector_t *connector)
5356 {
5457 bbsmnretriever_t *retriever;
5558
@@ -57,11 +60,9 @@
5760 if (retriever == NULL) {
5861 return NULL;
5962 }
60- retriever->retr = retriever_new();
61- if (retriever->retr == NULL) {
62- free(retriever);
63- return NULL;
64- }
63+ retriever->connector = connector;
64+ retriever->endpoint = -1;
65+ retriever->status = 0;
6566
6667 return retriever;
6768 }
@@ -68,34 +69,12 @@
6869
6970 EXPORT VOID bbsmnretriever_delete(bbsmnretriever_t *retriever)
7071 {
71- retriever_delete(retriever->retr);
72+ if (retriever->endpoint > 0) {
73+ http_connector_deleteendpoint(retriever->connector, retriever->endpoint);
74+ }
7275 free(retriever);
7376 }
7477
75-#define SO_ERR_SEND_LEN(sockID, str, len) \
76- err = so_send(sockID, (str), (len), 0); \
77- if(err < 0){ \
78- return err; \
79- }
80-
81-#define SO_ERR_SEND(sockID, str) SO_ERR_SEND_LEN(sockID, (str), strlen((str)))
82-
83-LOCAL W bbsmnretriever_sendheader(W sock)
84-{
85- W err;
86-
87- SO_ERR_SEND(sock, "GET /bbsmenu.html HTTP/1.1\r\n");
88- SO_ERR_SEND(sock, "Accept-Encoding: gzip\r\n");
89- SO_ERR_SEND(sock, "Host: menu.2ch.net\r\n");
90- SO_ERR_SEND(sock, "Accept: */*\r\n");
91- SO_ERR_SEND(sock, "Referer: http://menu.2ch.net/\r\n");
92- SO_ERR_SEND(sock, "Accept-Language: ja\r\n");
93- SO_ERR_SEND(sock, "User-Agent: Monazilla/1.00 (bchanl/0.101)\r\n");
94- SO_ERR_SEND(sock, "Connection: close\r\n");
95- SO_ERR_SEND(sock, "\r\n");
96-
97- return 0;
98-}
9978 /* from http://www.monazilla.org/index.php?e=196 */
10079 #if 0
10180 "
@@ -112,62 +91,80 @@
11291
11392 EXPORT W bbsmnretriever_sendrequest(bbsmnretriever_t *retriever, bbsmncache_t *cache)
11493 {
115- W sock, err, ret = -1, len, status;
116- UB *bin, *host = "menu.2ch.net";
94+ UB host[] = "menu.2ch.net";
11795
118- retriever_clearbuffer(retriever->retr);
119-
120- err = retriever_gethost(retriever->retr, host);
121- if (err < 0) {
122- return err;
96+ if (retriever->endpoint > 0) {
97+ DP(("bbsmnretriever_sendrequest: requesting\n"));
98+ return -1;
12399 }
124- sock = retriver_connectsocket(retriever->retr);
125- if (sock < 0) {
126- return sock;
127- }
128100
129- err = bbsmnretriever_sendheader(sock);
130- if (err < 0) {
131- so_close(sock);
132- return err;
101+ retriever->endpoint = http_connector_createendpoint(retriever->connector, host, strlen(host), 80, HTTP_METHOD_GET);
102+ if (retriever->endpoint < 0) {
103+ DP_ER("http_connector_createendpoint error", retriever->endpoint);
104+ return -1;
133105 }
134106
135- err = retriever_recieve(retriever->retr, sock);
136- if (err < 0) {
137- so_close(sock);
138- return err;
139- }
107+ return 0;
108+}
140109
141- err = retriever_parsehttpresponse(retriever->retr);
142- if (err < 0) {
143- so_close(sock);
144- return err;
110+EXPORT Bool bbsmnretriever_iswaitingendpoint(bbsmnretriever_t *retriever, ID endpoint)
111+{
112+ if (retriever->endpoint == endpoint) {
113+ return True;
145114 }
146- retriever_dumpheader(retriever->retr);
115+ return False;
116+}
147117
148- err = retriever_decompress(retriever->retr);
149- if (err < 0) {
150- so_close(sock);
151- return err;
152- }
118+LOCAL UB path[] = "/bbsmenu.html";
119+LOCAL UB header[] =
120+"Accept: */*\r\n"
121+"Referer: http://menu.2ch.net/\r\n"
122+"Accept-Language: ja\r\n"
123+"User-Agent: Monazilla/1.00\r\n";
153124
154- status = retriever_parse_response_status(retriever->retr);
155- if (status == 200) {
156- bin = retriever_getbody(retriever->retr);
157- len = retriever_getbodylength(retriever->retr);
158- bbsmncache_cleardata(cache);
159- bbsmncache_appenddata(cache, bin, len);
125+EXPORT W bbsmnretriever_recievehttpevent(bbsmnretriever_t *retriever, bbsmncache_t *cache, http_connector_event *hevent)
126+{
127+ http_connector_t *connector = retriever->connector;
160128
161- bin = retriever_getheader(retriever->retr);
162- len = retriever_getheaderlength(retriever->retr);
163- bbsmncache_updatelatestheader(cache, bin, len);
129+ if (retriever->endpoint <= 0) {
130+ return -1;
131+ }
164132
165- ret = BBSMNRETRIEVER_REQUEST_ALLRELOAD;
133+ if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_SEND) {
134+ http_connector_sendrequestline(connector, hevent->endpoint, path, strlen(path));
135+ http_connector_sendheader(connector, hevent->endpoint, header, strlen(header));
136+ http_connector_sendheaderend(connector, hevent->endpoint);
137+ http_connector_sendmessagebody(connector, hevent->endpoint, NULL, 0);
138+ http_connector_sendmessagebodyend(connector, hevent->endpoint);
139+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_STATUSLINE) {
140+ DP(("HTTP_CONNECTOR_EVENTTYPE_RECEIVE_STATUSLINE\n"));
141+ DP((" status = %d\n", hevent->data.receive_statusline.statuscode));
142+ retriever->status = hevent->data.receive_statusline.statuscode;
143+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_HEADER) {
144+#ifdef BCHANL_CONFIG_DEBUG
145+ {
146+ W i = 0;
147+ for (i = 0; i < hevent->data.receive_header.len; i++) {
148+ printf("%c", hevent->data.receive_header.bin[i]);
149+ }
150+ }
151+#endif
152+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_HEADER_END) {
153+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_MESSAGEBODY) {
154+ if (retriever->status == HTTP_STATUSCODE_200_OK) {
155+ bbsmncache_appenddata(cache, hevent->data.receive_messagebody.bin, hevent->data.receive_messagebody.len);
156+ }
157+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_MESSAGEBODY_END) {
158+ http_connector_deleteendpoint(connector, hevent->endpoint);
159+ retriever->endpoint = -1;
160+ return BBSMNRETRIEVER_REQUEST_ALLRELOAD;
161+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_ERROR) {
162+ http_connector_deleteendpoint(connector, hevent->endpoint);
163+ retriever->endpoint = -1;
164+ } else {
165+ /* error */
166+ return -1;
166167 }
167168
168- so_close(sock);
169-
170- retriever_clearbuffer(retriever->retr);
171-
172- return ret;
169+ return BBSMNRETRIEVER_REQUEST_WAITNEXT;
173170 }
--- bchanl/trunk/src/bbsmenuretriever.h (revision 504)
+++ bchanl/trunk/src/bbsmenuretriever.h (revision 505)
@@ -1,7 +1,7 @@
11 /*
22 * bbsmenuretriever.h
33 *
4- * Copyright (c) 2009 project bchan
4+ * Copyright (c) 2009-2012 project bchan
55 *
66 * This software is provided 'as-is', without any express or implied
77 * warranty. In no event will the authors be held liable for any damages
@@ -26,6 +26,7 @@
2626
2727 #include <basic.h>
2828 #include "bbsmenucache.h"
29+#include <http/http_connector.h>
2930
3031 #ifndef __BBSMENURETREIEVER_H__
3132 #define __BBSMENURETREIEVER_H__
@@ -32,11 +33,14 @@
3233
3334 typedef struct bbsmnretriever_t_ bbsmnretriever_t;
3435
35-IMPORT bbsmnretriever_t* bbsmnretriever_new();
36+IMPORT bbsmnretriever_t* bbsmnretriever_new(http_connector_t *connector);
3637 IMPORT VOID bbsmnretriever_delete(bbsmnretriever_t *retriever);
3738 IMPORT W bbsmnretriever_sendrequest(bbsmnretriever_t *retriever, bbsmncache_t *cache);
39+IMPORT Bool bbsmnretriever_iswaitingendpoint(bbsmnretriever_t *retriever, ID endpoint);
40+IMPORT W bbsmnretriever_recievehttpevent(bbsmnretriever_t *retriever, bbsmncache_t *cache, http_connector_event *hevent);
3841
3942 #define BBSMNRETRIEVER_REQUEST_NOT_MODIFIED 0
4043 #define BBSMNRETRIEVER_REQUEST_ALLRELOAD 1
44+#define BBSMNRETRIEVER_REQUEST_WAITNEXT 2
4145
4246 #endif
--- bchanl/trunk/src/main.c (revision 504)
+++ bchanl/trunk/src/main.c (revision 505)
@@ -58,6 +58,8 @@
5858 #include "bchanl_menus.h"
5959 #include "bchanl_panels.h"
6060
61+#include <http/http_connector.h>
62+
6163 #ifdef BCHANL_CONFIG_DEBUG
6264 # define DP(arg) printf arg
6365 # define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
@@ -144,9 +146,11 @@
144146 TC *category_extbbs;
145147 };
146148
149+#define BCHANL_NETWORK_FLAG_WAITHTTPEVENT 0x00000001
150+
147151 struct bchanl_t_ {
148152 W taskid;
149- W mbfid;
153+ W flgid; /* for reduce TMOUT message sending. */
150154
151155 bchanl_mainmenu_t mainmenu;
152156 VID vid;
@@ -154,6 +158,8 @@
154158
155159 bchanl_hmistate_t hmistate;
156160
161+ http_connector_t *connector;
162+
157163 sbjtretriever_t *retriever;
158164
159165 bchanl_subjecthash_t *subjecthash;
@@ -659,7 +665,7 @@
659665 }
660666 }
661667
662-LOCAL W bchanl_bbsmenu_initialize(bchanl_bbsmenu_t *bchanl, GID gid, bchanl_subjecthash_t *subjecthash, LINK *storage)
668+LOCAL W bchanl_bbsmenu_initialize(bchanl_bbsmenu_t *bchanl, GID gid, bchanl_subjecthash_t *subjecthash, LINK *storage, http_connector_t *connector)
663669 {
664670 bbsmnretriever_t *retriever;
665671 bbsmncache_t *cache;
@@ -675,7 +681,7 @@
675681 if (cache == NULL) {
676682 goto error_cache;
677683 }
678- retriever = bbsmnretriever_new();
684+ retriever = bbsmnretriever_new(connector);
679685 if (retriever == NULL) {
680686 goto error_retriever;
681687 }
@@ -1007,42 +1013,80 @@
10071013
10081014 #define BCHANL_MESSAGE_RETRIEVER_RELAYOUT 1
10091015 #define BCHANL_MESSAGE_RETRIEVER_ERROR -1
1016+#define BCHANL_MESSAGE_HTTP_EVENT 2
10101017
1011-LOCAL VOID bchanl_retriever_task(W arg)
1018+LOCAL Bool bchanl_bbsmenu_httpevent(bchanl_bbsmenu_t *bchanl, http_connector_event *hevent)
10121019 {
1020+ Bool ok;
1021+ W err;
1022+
1023+ ok = bbsmnretriever_iswaitingendpoint(bchanl->retriever, hevent->endpoint);
1024+ if (ok == False) {
1025+ return False;
1026+ }
1027+ err = bbsmnretriever_recievehttpevent(bchanl->retriever, bchanl->cache, hevent);
1028+
1029+ switch (err) {
1030+ case BBSMNRETRIEVER_REQUEST_ALLRELOAD:
1031+ req_tmg(0, BCHANL_MESSAGE_RETRIEVER_RELAYOUT);
1032+ break;
1033+ case BBSMNRETRIEVER_REQUEST_WAITNEXT:
1034+ break;
1035+ default:
1036+ req_tmg(0, BCHANL_MESSAGE_RETRIEVER_ERROR);
1037+ DP_ER("bbsmnretriever_recievehttpevent", err);
1038+ break;
1039+ }
1040+
1041+ return True;
1042+}
1043+
1044+LOCAL VOID bchanl_http_task(W arg)
1045+{
10131046 bchanl_t *bchanl;
1047+ http_connector_t *connector;
10141048 bbsmnretriever_t *retr;
10151049 bbsmncache_t *cache;
1016- W msg,err;
1050+ W err;
10171051
10181052 bchanl = (bchanl_t*)arg;
1053+ connector = bchanl->connector;
10191054 retr = bchanl->bbsmenu.retriever;
10201055 cache = bchanl->bbsmenu.cache;
10211056
10221057 for (;;) {
1023- DP(("before rcv_mbf %d\n", bchanl->mbfid));
1024- err = rcv_mbf(bchanl->mbfid, (VP)&msg, T_FOREVER);
1025- DP_ER("rcv_mbf error:",err);
1026- if (err != 4) {
1027- continue;
1028- }
1029-
1030- err = bbsmnretriever_sendrequest(retr, cache);
1031-
1032- switch (err) {
1033- case BBSMNRETRIEVER_REQUEST_ALLRELOAD:
1034- req_tmg(0, BCHANL_MESSAGE_RETRIEVER_RELAYOUT);
1035- break;
1036- default:
1058+ err = http_connector_waitconnection(connector, T_FOREVER);
1059+ if (err < 0) {
1060+ DP_ER("http_connector_waitconnection", err);
10371061 req_tmg(0, BCHANL_MESSAGE_RETRIEVER_ERROR);
1038- DP_ER("bbsmnretreiver_request error",err);
10391062 break;
10401063 }
1064+
1065+ err = wai_flg(bchanl->flgid, BCHANL_NETWORK_FLAG_WAITHTTPEVENT, WF_AND, T_FOREVER);
1066+ if (err < 0) {
1067+ DP_ER("wai_flg", err);
1068+ }
1069+ req_tmg(0, BCHANL_MESSAGE_HTTP_EVENT);
10411070 }
10421071
10431072 ext_tsk();
10441073 }
10451074
1075+LOCAL VOID bchanl_handle_httpevent(bchanl_t *bchanl)
1076+{
1077+ W err;
1078+ http_connector_event hevent;
1079+
1080+ set_flg(bchanl->flgid, BCHANL_NETWORK_FLAG_WAITHTTPEVENT);
1081+
1082+ err = http_connector_getevent(bchanl->connector, &hevent);
1083+ if (err < 0) {
1084+ return;
1085+ }
1086+
1087+ bchanl_bbsmenu_httpevent(&bchanl->bbsmenu, &hevent);
1088+}
1089+
10461090 LOCAL W bchanl_prepare_network(bchanl_t *bchanl)
10471091 {
10481092 if (bchanl->retriever == NULL) {
@@ -1049,18 +1093,18 @@
10491093 return 0;
10501094 }
10511095
1052- bchanl->mbfid = cre_mbf(sizeof(W), sizeof(W), DELEXIT);
1053- if (bchanl->mbfid < 0) {
1054- DP_ER("error cre_mbf:", bchanl->mbfid);
1055- return -1;
1056- }
1057- bchanl->taskid = cre_tsk(bchanl_retriever_task, -1, (W)bchanl);
1096+ bchanl->taskid = cre_tsk(bchanl_http_task, -1, (W)bchanl);
10581097 if (bchanl->taskid < 0) {
1059- del_mbf(bchanl->mbfid);
1060- bchanl->mbfid = -1;
10611098 DP_ER("error cre_tsk:", bchanl->taskid);
10621099 return -1;
10631100 }
1101+ bchanl->flgid = cre_flg(0, DELEXIT);
1102+ if (bchanl->flgid < 0) {
1103+ ter_tsk(bchanl->taskid);
1104+ bchanl->taskid = -1;
1105+ DP_ER("error cre_flg:", bchanl->flgid);
1106+ return -1;
1107+ }
10641108
10651109 return 0;
10661110 }
@@ -1067,11 +1111,11 @@
10671111
10681112 LOCAL W bchanl_networkrequest_bbsmenu(bchanl_t *bchanl)
10691113 {
1070- W msg = 1, err;
1114+ W err;
10711115 static UW lastrequest = 0;
10721116 UW etime;
10731117
1074- if (bchanl->mbfid < 0) {
1118+ if (bchanl->flgid < 0) {
10751119 return 0;
10761120 }
10771121
@@ -1085,11 +1129,15 @@
10851129 }
10861130 lastrequest = etime;
10871131
1088- err = snd_mbf(bchanl->mbfid, &msg, sizeof(W), T_FOREVER);
1132+ bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_BUSY);
1133+
1134+ err = bbsmnretriever_sendrequest(bchanl->bbsmenu.retriever, bchanl->bbsmenu.cache);
10891135 if (err < 0) {
1090- DP_ER("snd_mbf error:", err);
1136+ DP_ER("bbsmnretriever_sendrequest error:", err);
1137+ bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_SELECT);
10911138 return err;
10921139 }
1140+ set_flg(bchanl->flgid, BCHANL_NETWORK_FLAG_WAITHTTPEVENT);
10931141
10941142 bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_BUSY);
10951143 pdsp_msg(bchanl->hmistate.msg_retr_bbsmenu);
@@ -1111,6 +1159,7 @@
11111159 GID gid;
11121160 RECT w_work;
11131161 PNT p0 = {450, 0};
1162+ http_connector_t *connector;
11141163 sbjtretriever_t *retriever;
11151164 bchanlhmi_t *hmi;
11161165 bchanl_subjecthash_t *subjecthash;
@@ -1127,6 +1176,12 @@
11271176 bgpat = &bgpat0;
11281177 }
11291178
1179+ connector = http_connector_new();
1180+ if (connector == NULL) {
1181+ DP_ER("http_connector_new error", 0);
1182+ goto error_http_connector;
1183+ }
1184+
11301185 retriever = sbjtretriever_new();
11311186 if (retriever == NULL) {
11321187 DP_ER("sbjtretriever_new error", 0);
@@ -1171,7 +1226,7 @@
11711226 DP_ER("bchanlhmi_newexternalbbswindow", 0);
11721227 goto error_externalbbswindow;
11731228 }
1174- err = bchanl_bbsmenu_initialize(&(bchanl->bbsmenu), gid, subjecthash, storage);
1229+ err = bchanl_bbsmenu_initialize(&(bchanl->bbsmenu), gid, subjecthash, storage, connector);
11751230 if (err < 0) {
11761231 DP_ER("bchanl_bbsmenu_initialize error", err);
11771232 goto error_bbsmenu;
@@ -1193,6 +1248,7 @@
11931248 bbsmndraw_setviewrect(bchanl->bbsmenu.draw, 0, 0, w_work.c.right, w_work.c.bottom);
11941249 bbsmenuwindow_setworkrect(bbsmenuwindow, 0, 0, w_work.c.right, w_work.c.bottom);
11951250
1251+ bchanl->connector = connector;
11961252 bchanl->retriever = retriever;
11971253 bchanl->subjecthash = subjecthash;
11981254
@@ -1232,6 +1288,8 @@
12321288 error_bchanlhmi:
12331289 sbjtretriever_delete(retriever);
12341290 error_retriever:
1291+ http_connector_delete(connector);
1292+error_http_connector:
12351293 return -1; /* TODO */
12361294 }
12371295
@@ -1253,6 +1311,7 @@
12531311 bchanlhmi_deletesubjectwindow(bchanl->hmi, bchanl->subjectwindow);
12541312 bchanlhmi_delete(bchanl->hmi);
12551313 sbjtretriever_delete(bchanl->retriever);
1314+ http_connector_delete(bchanl->connector);
12561315
12571316 ext_prc(0);
12581317 }
@@ -1803,6 +1862,9 @@
18031862 bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_SELECT);
18041863 pdsp_msg(NULL);
18051864 break;
1865+ case BCHANL_MESSAGE_HTTP_EVENT:
1866+ bchanl_handle_httpevent(bchanl);
1867+ break;
18061868 }
18071869 }
18081870
Show on old repository browser