• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂507 (tree)
時間2012-06-23 18:23:29
作者ornse01

Log Message

replace http request implement for subjectretriever.

Change Summary

差異

--- bchanl/trunk/src/main.c (revision 506)
+++ bchanl/trunk/src/main.c (revision 507)
@@ -165,6 +165,7 @@
165165 bchanl_subjecthash_t *subjecthash;
166166 bchanl_bbsmenu_t bbsmenu;
167167 bchanl_subject_t *currentsubject;
168+ bchanl_subject_t *nextsubject;
168169 struct {
169170 Bool resnum;
170171 Bool since;
@@ -425,6 +426,11 @@
425426 subjectwindow_requestredisp(bchanl->subjectwindow);
426427 }
427428
429+LOCAL VOID bchanl_setnextsubject(bchanl_t *bchanl, bchanl_subject_t *sbjt)
430+{
431+ bchanl->nextsubject = sbjt;
432+}
433+
428434 LOCAL VOID bchanl_bbsmenuwindow_draw(bchanl_t *bchanl)
429435 {
430436 RECT r;
@@ -582,11 +588,7 @@
582588 LOCAL VOID bchanl_sendsubjectrequest(bchanl_t *bchanl, bchanl_subject_t *subject)
583589 {
584590 sbjtcache_t *cache;
585- sbjtlayout_t *layout;
586- sbjtdraw_t *draw;
587- TC *title;
588- RECT w_work;
589- W l, t, r, b, title_len, err;
591+ W err;
590592
591593 bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_BUSY);
592594 pdsp_msg(bchanl->hmistate.msg_retr_subject);
@@ -598,29 +600,8 @@
598600 bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_SELECT);
599601 return;
600602 }
601-
602- pdsp_msg(NULL);
603- bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_SELECT);
604-
605- subjectoptionwindow_setfiltertext(bchanl->subjectoptionwindow, NULL, 0);
606- err = subjectoptionwindow_setordervalue(bchanl->subjectoptionwindow, SUBJECTOPTIONWINDOW_ORDERVALUE_ASCENDING);
607- subjectoptionwindow_setorderbyvalue(bchanl->subjectoptionwindow, SUBJECTOPTIONWINDOW_ORDERBYVALUE_NUMBER);
608-
609- bchanl_subject_relayout(subject);
610-
611- bchanl_setcurrentsubject(bchanl, subject);
612-
613- subjectwindow_getworkrect(bchanl->subjectwindow, &w_work);
614- draw = bchanl_subject_getdraw(subject);
615- sbjtdraw_setviewrect(draw, 0, 0, w_work.c.right, w_work.c.bottom);
616- subjectwindow_setworkrect(bchanl->subjectwindow, 0, 0, w_work.c.right, w_work.c.bottom);
617-
618- layout = bchanl_subject_getlayout(subject);
619- sbjtlayout_getdrawrect(layout, &l, &t, &r, &b);
620- subjectwindow_setdrawrect(bchanl->subjectwindow, l, t, r, b);
621-
622- bchanl_subject_gettitle(subject, &title, &title_len);
623- subjectwindow_settitle(bchanl->subjectwindow, title);
603+ bchanl_setnextsubject(bchanl, subject);
604+ set_flg(bchanl->flgid, BCHANL_NETWORK_FLAG_WAITHTTPEVENT);
624605 }
625606
626607 LOCAL VOID bchanl_bbsmenuwindow_click(bchanl_t *bchanl, PNT pos)
@@ -1041,6 +1022,69 @@
10411022 return True;
10421023 }
10431024
1025+LOCAL Bool bchanl_subject_httpevent(bchanl_t *bchanl, http_connector_event *hevent)
1026+{
1027+ Bool ok;
1028+ W err;
1029+ sbjtcache_t *cache;
1030+ sbjtlayout_t *layout;
1031+ sbjtdraw_t *draw;
1032+ TC *title;
1033+ RECT w_work;
1034+ W l, t, r, b, title_len;
1035+
1036+ if (bchanl->nextsubject == NULL) {
1037+ return False;
1038+ }
1039+
1040+ ok = sbjtretriever_iswaitingendpoint(bchanl->retriever, hevent->endpoint);
1041+ if (ok == False) {
1042+ return False;
1043+ }
1044+ cache = bchanl_subject_getcache(bchanl->nextsubject);
1045+ err = sbjtretriever_recievehttpevent(bchanl->retriever, cache, hevent);
1046+
1047+ switch (err) {
1048+ case SBJTRETRIEVER_REQUEST_ALLRELOAD:
1049+ /* should asynchronous layout? */
1050+
1051+ pdsp_msg(NULL);
1052+ bchanl_hmistate_updateptrstyle(&bchanl->hmistate, PS_SELECT);
1053+
1054+ subjectoptionwindow_setfiltertext(bchanl->subjectoptionwindow, NULL, 0);
1055+ err = subjectoptionwindow_setordervalue(bchanl->subjectoptionwindow, SUBJECTOPTIONWINDOW_ORDERVALUE_ASCENDING);
1056+ subjectoptionwindow_setorderbyvalue(bchanl->subjectoptionwindow, SUBJECTOPTIONWINDOW_ORDERBYVALUE_NUMBER);
1057+
1058+
1059+ bchanl_subject_relayout(bchanl->nextsubject);
1060+
1061+ bchanl_setcurrentsubject(bchanl, bchanl->nextsubject);
1062+ bchanl_setnextsubject(bchanl, NULL);
1063+
1064+ subjectwindow_getworkrect(bchanl->subjectwindow, &w_work);
1065+ draw = bchanl_subject_getdraw(bchanl->currentsubject);
1066+ sbjtdraw_setviewrect(draw, 0, 0, w_work.c.right, w_work.c.bottom);
1067+ subjectwindow_setworkrect(bchanl->subjectwindow, 0, 0, w_work.c.right, w_work.c.bottom);
1068+
1069+ layout = bchanl_subject_getlayout(bchanl->currentsubject);
1070+ sbjtlayout_getdrawrect(layout, &l, &t, &r, &b);
1071+ subjectwindow_setdrawrect(bchanl->subjectwindow, l, t, r, b);
1072+
1073+ bchanl_subject_gettitle(bchanl->currentsubject, &title, &title_len);
1074+ subjectwindow_settitle(bchanl->subjectwindow, title);
1075+
1076+ break;
1077+ case SBJTRETRIEVER_REQUEST_WAITNEXT:
1078+ break;
1079+ default:
1080+ req_tmg(0, BCHANL_MESSAGE_RETRIEVER_ERROR);
1081+ DP_ER("bbsmnretriever_recievehttpevent", err);
1082+ break;
1083+ }
1084+
1085+ return True;
1086+}
1087+
10441088 LOCAL VOID bchanl_http_task(W arg)
10451089 {
10461090 bchanl_t *bchanl;
@@ -1076,6 +1120,7 @@
10761120 {
10771121 W err;
10781122 http_connector_event hevent;
1123+ Bool rcv;
10791124
10801125 set_flg(bchanl->flgid, BCHANL_NETWORK_FLAG_WAITHTTPEVENT);
10811126
@@ -1084,7 +1129,12 @@
10841129 return;
10851130 }
10861131
1087- bchanl_bbsmenu_httpevent(&bchanl->bbsmenu, &hevent);
1132+ rcv = bchanl_bbsmenu_httpevent(&bchanl->bbsmenu, &hevent);
1133+ if (rcv != False) {
1134+ return;
1135+ }
1136+
1137+ rcv = bchanl_subject_httpevent(bchanl, &hevent);
10881138 }
10891139
10901140 LOCAL W bchanl_prepare_network(bchanl_t *bchanl)
@@ -1182,7 +1232,7 @@
11821232 goto error_http_connector;
11831233 }
11841234
1185- retriever = sbjtretriever_new();
1235+ retriever = sbjtretriever_new(connector);
11861236 if (retriever == NULL) {
11871237 DP_ER("sbjtretriever_new error", 0);
11881238 goto error_retriever;
@@ -1253,6 +1303,7 @@
12531303 bchanl->subjecthash = subjecthash;
12541304
12551305 bchanl->currentsubject = NULL;
1306+ bchanl->nextsubject = NULL;
12561307 bchanl->subjectdisplay.resnum = True;
12571308 bchanl->subjectdisplay.since = False;
12581309 bchanl->subjectdisplay.vigor = False;
--- bchanl/trunk/src/subjectretriever.c (revision 506)
+++ bchanl/trunk/src/subjectretriever.c (revision 507)
@@ -1,7 +1,7 @@
11 /*
22 * subjectretriever.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 "subjectretriever.h"
3636
37-#include "retriever.h"
3837 #include "subjectcache.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 sbjtretriever_t_ {
49- retriever_t *retr;
50+ http_connector_t *connector;
51+ ID endpoint;
52+ HTTP_STATUSCODE status;
5053 };
5154
52-EXPORT sbjtretriever_t* sbjtretriever_new()
55+EXPORT sbjtretriever_t* sbjtretriever_new(http_connector_t *connector)
5356 {
5457 sbjtretriever_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,40 +69,12 @@
6869
6970 EXPORT VOID sbjtretriever_delete(sbjtretriever_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 sbjtretriver_sendheader(W sock, UB *host, UB *board)
84-{
85- W err;
86-
87- SO_ERR_SEND(sock, "GET /");
88- SO_ERR_SEND(sock, board);
89- SO_ERR_SEND(sock, "/subject.txt HTTP/1.1\r\n");
90- SO_ERR_SEND(sock, "Accept-Encoding: gzip\r\n");
91- SO_ERR_SEND(sock, "HOST: ");
92- SO_ERR_SEND(sock, host);
93- SO_ERR_SEND(sock, "\r\n");
94- SO_ERR_SEND(sock, "Accept: */*\r\n");
95- SO_ERR_SEND(sock, "Referer: http://");
96- SO_ERR_SEND(sock, host);
97- SO_ERR_SEND(sock, "/");
98- SO_ERR_SEND(sock, board);
99- SO_ERR_SEND(sock, "/");
100- SO_ERR_SEND(sock, "/\r\n");
101- SO_ERR_SEND(sock, "Accept-Language: ja\r\nUser-Agent: Monazilla/1.00 (bchanl/0.101)\r\nConnection: close\r\n\r\n");
102-
103- return 0;
104-}
10578 /* from http://www.monazilla.org/index.php?e=197 */
10679 #if 0
10780 "GET /[板名]/subject.txt HTTP/1.1
@@ -119,60 +92,107 @@
11992
12093 EXPORT W sbjtretriever_sendrequest(sbjtretriever_t *retriever, sbjtcache_t *cache)
12194 {
122- W sock, err, ret = -1, len, status, host_len, board_len;
123- UB *bin, *host, *board;
95+ W host_len;
96+ UB *host;
12497
125- retriever_clearbuffer(retriever->retr);
98+ if (retriever->endpoint > 0) {
99+ DP(("sbjtretriever_sendrequest: requesting\n"));
100+ return -1;
101+ }
126102
127103 sbjtcache_gethost(cache, &host, &host_len);
128- sbjtcache_getboard(cache, &board, &board_len);
129104
130- err = retriever_gethost(retriever->retr, host);
131- if (err < 0) {
132- return err;
105+ retriever->endpoint = http_connector_createendpoint(retriever->connector, host, host_len, 80, HTTP_METHOD_GET);
106+ if (retriever->endpoint < 0) {
107+ DP_ER("http_connector_createendpoint error", retriever->endpoint);
108+ return -1;
133109 }
134- sock = retriver_connectsocket(retriever->retr);
135- if (sock < 0) {
136- return sock;
137- }
138110
139- err = sbjtretriver_sendheader(sock, host, board);
140- if (err < 0) {
141- so_close(sock);
142- return err;
143- }
111+ return 0;
112+}
144113
145- err = retriever_recieve(retriever->retr, sock);
146- if (err < 0) {
147- so_close(sock);
148- return err;
114+EXPORT Bool sbjtretriever_iswaitingendpoint(sbjtretriever_t *retriever, ID endpoint)
115+{
116+ if (retriever->endpoint == endpoint) {
117+ return True;
149118 }
119+ return False;
120+}
150121
151- err = retriever_parsehttpresponse(retriever->retr);
152- if (err < 0) {
153- so_close(sock);
154- return err;
155- }
156- retriever_dumpheader(retriever->retr);
122+LOCAL UB header1[] =
123+"Accept: */*\r\n"
124+"Referer: http://";
125+LOCAL UB header2[] =
126+"/\r\n"
127+"Accept-Language: ja\r\n"
128+"User-Agent: Monazilla/1.00\r\n";
157129
158- err = retriever_decompress(retriever->retr);
159- if (err < 0) {
160- so_close(sock);
161- return err;
162- }
130+EXPORT W sbjtretriever_recievehttpevent(sbjtretriever_t *retriever, sbjtcache_t *cache, http_connector_event *hevent)
131+{
132+ http_connector_t *connector = retriever->connector;
133+ W host_len, board_len, path_len;
134+ UB *host, *board, *path;
163135
164- status = retriever_parse_response_status(retriever->retr);
165- if (status == 200) {
166- bin = retriever_getbody(retriever->retr);
167- len = retriever_getbodylength(retriever->retr);
168- sbjtcache_cleardata(cache);
169- sbjtcache_appenddata(cache, bin, len);
170- ret = 0;
136+ if (retriever->endpoint <= 0) {
137+ return -1;
171138 }
172139
173- so_close(sock);
140+ if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_SEND) {
141+ sbjtcache_getboard(cache, &host, &host_len);
142+ sbjtcache_getboard(cache, &board, &board_len);
174143
175- retriever_clearbuffer(retriever->retr);
144+ path_len = 1 + board_len + 12;
145+ path = malloc(sizeof(UB)*(path_len+1));
146+ if (path == NULL) {
147+ return -1;
148+ }
149+ path[0] = '/';
150+ strncpy(path+1, board, board_len);
151+ strncpy(path+1+board_len, "/subject.txt", 12);
152+ path[1+board_len+12] = '\0';
153+ http_connector_sendrequestline(connector, hevent->endpoint, path, path_len);
154+ free(path);
176155
177- return ret;
156+ http_connector_sendheader(connector, hevent->endpoint, header1, strlen(header1));
157+ http_connector_sendheader(connector, hevent->endpoint, host, host_len);
158+ http_connector_sendheader(connector, hevent->endpoint, "/", 1);
159+ http_connector_sendheader(connector, hevent->endpoint, board, board_len);
160+ http_connector_sendheader(connector, hevent->endpoint, header2, strlen(header2));
161+ http_connector_sendheaderend(connector, hevent->endpoint);
162+ http_connector_sendmessagebody(connector, hevent->endpoint, NULL, 0);
163+ http_connector_sendmessagebodyend(connector, hevent->endpoint);
164+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_STATUSLINE) {
165+ DP(("HTTP_CONNECTOR_EVENTTYPE_RECEIVE_STATUSLINE\n"));
166+ DP((" status = %d\n", hevent->data.receive_statusline.statuscode));
167+ retriever->status = hevent->data.receive_statusline.statuscode;
168+ if (retriever->status == HTTP_STATUSCODE_200_OK) {
169+ sbjtcache_cleardata(cache);
170+ }
171+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_HEADER) {
172+#ifdef BCHANL_CONFIG_DEBUG
173+ {
174+ W i = 0;
175+ for (i = 0; i < hevent->data.receive_header.len; i++) {
176+ printf("%c", hevent->data.receive_header.bin[i]);
177+ }
178+ }
179+#endif
180+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_HEADER_END) {
181+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_MESSAGEBODY) {
182+ if (retriever->status == HTTP_STATUSCODE_200_OK) {
183+ sbjtcache_appenddata(cache, hevent->data.receive_messagebody.bin, hevent->data.receive_messagebody.len);
184+ }
185+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_RECEIVE_MESSAGEBODY_END) {
186+ http_connector_deleteendpoint(connector, hevent->endpoint);
187+ retriever->endpoint = -1;
188+ return SBJTRETRIEVER_REQUEST_ALLRELOAD;
189+ } else if (hevent->type == HTTP_CONNECTOR_EVENTTYPE_ERROR) {
190+ http_connector_deleteendpoint(connector, hevent->endpoint);
191+ retriever->endpoint = -1;
192+ } else {
193+ /* error */
194+ return -1;
195+ }
196+
197+ return SBJTRETRIEVER_REQUEST_WAITNEXT;
178198 }
--- bchanl/trunk/src/subjectretriever.h (revision 506)
+++ bchanl/trunk/src/subjectretriever.h (revision 507)
@@ -1,7 +1,7 @@
11 /*
22 * subjectretriever.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 "subjectcache.h"
29+#include <http/http_connector.h>
2930
3031 #ifndef __SUBJECTRETREIEVER_H__
3132 #define __SUBJECTRETREIEVER_H__
@@ -32,8 +33,14 @@
3233
3334 typedef struct sbjtretriever_t_ sbjtretriever_t;
3435
35-IMPORT sbjtretriever_t* sbjtretriever_new();
36+IMPORT sbjtretriever_t* sbjtretriever_new(http_connector_t *connector);
3637 IMPORT VOID sbjtretriever_delete(sbjtretriever_t *retriever);
3738 IMPORT W sbjtretriever_sendrequest(sbjtretriever_t *retriever, sbjtcache_t *cache);
39+IMPORT Bool sbjtretriever_iswaitingendpoint(sbjtretriever_t *retriever, ID endpoint);
40+IMPORT W sbjtretriever_recievehttpevent(sbjtretriever_t *retriever, sbjtcache_t *cache, http_connector_event *hevent);
3841
42+#define SBJTRETRIEVER_REQUEST_NOT_MODIFIED 0
43+#define SBJTRETRIEVER_REQUEST_ALLRELOAD 1
44+#define SBJTRETRIEVER_REQUEST_WAITNEXT 2
45+
3946 #endif
Show on old repository browser