http/fetch: call callback even if status is not OK so errors can be handled; surf: added back dest param for cmd()

This commit is contained in:
Gabor Adam Toth 2010-02-23 20:34:26 +01:00
parent 7d715092c1
commit c6264e1087
4 changed files with 35 additions and 27 deletions

View File

@ -1352,8 +1352,9 @@ _PAGES_start_description
|<script> |<script>
|var longer = 44 |var longer = 44
| |
|function cmd(wish) { |function cmd(wish, dest) {
| Pef.cmd.value = wish | Pef.cmd.value = wish
| Pef.dest.value = dest || ''
| Pef.submit() | Pef.submit()
| longer = 44 | longer = 44
| return false | return false

View File

@ -202,15 +202,14 @@ disconnected(remainder) {
fheaders = headers; fheaders = headers;
buffer = headers = 0; buffer = headers = 0;
switch (http_status) { switch (http_status) {
case R_OK: default:
mixed *waiter; mixed *waiter;
while (qSize(ME)) { while (qSize(ME)) {
waiter = shift(ME); waiter = shift(ME);
P2(("%O calls back.. body is %O\n", ME, fetched)) P2(("%O calls back.. body is %O\n", ME, fetched))
funcall(waiter[0], fetched, waiter[1] ? fheaders : copy(fheaders)); funcall(waiter[0], fetched, waiter[1] ? fheaders : copy(fheaders), http_status);
} }
break; if (http_status == R_OK) break;
default:
// doesn't seem to get here when HTTP returns 301 or 302. strange. // doesn't seem to get here when HTTP returns 301 or 302. strange.
// fall thru // fall thru
case R_NOTMODIFIED: case R_NOTMODIFIED:

View File

@ -9,6 +9,7 @@
#include <net.h> #include <net.h>
#include <tls.h> #include <tls.h>
#include <ht/http.h>
string consumer_key; string consumer_key;
string consumer_secret; string consumer_secret;
@ -51,28 +52,32 @@ varargs void fetch(object ua, string url, string method, mapping post, mapping o
ua->fetch(url, method, post, (["authorization": "OAuth " + p])); ua->fetch(url, method, post, (["authorization": "OAuth " + p]));
} }
void parse_request_token(string body, mapping headers) { void parse_request_token(string body, mapping headers, int http_status) {
P3((">> oauth:parse_request_token(%O, %O)\n", body, headers)) P3((">> oauth:parse_request_token(%O, %O, %O)\n", body, headers, http_status))
request_params = ([]); if (http_status == R_OK) {
url_parse_query(request_params, body); request_params = ([]);
if (strlen(request_params["oauth_token"]) && strlen(request_params["oauth_token_secret"])) { url_parse_query(request_params, body);
shared_memory("oauth_request_tokens")[request_params["oauth_token"]] = ME; if (strlen(request_params["oauth_token"]) && strlen(request_params["oauth_token_secret"])) {
sendmsg(user, "_notice_oauth_authorize_url", "Open [_url] to perform authorization.", shared_memory("oauth_request_tokens")[request_params["oauth_token"]] = ME;
(["_url": authorize_url + "?oauth_token=" + request_params["oauth_token"]])); sendmsg(user, "_notice_oauth_authorize_url", "Open [_url] to perform authorization.",
} else { (["_url": authorize_url + "?oauth_token=" + request_params["oauth_token"]]));
sendmsg(user, "_error_oauth_token_request", "OAuth failed: could not get a request token."); return;
}
} }
sendmsg(user, "_error_oauth_token_request", "OAuth failed: could not get a request token.");
} }
void parse_access_token(string body, mapping headers) { void parse_access_token(string body, mapping headers, int http_status) {
P3((">> oauth:parse_access_token(%O, %O)\n", body, headers)) P3((">> oauth:parse_access_token(%O, %O, %O)\n", body, headers, http_status))
access_params = ([]); if (http_status == R_OK) {
url_parse_query(access_params, body); access_params = ([]);
if (strlen(access_params["oauth_token"]) && strlen(access_params["oauth_token_secret"])) { url_parse_query(access_params, body);
sendmsg(user, "_notice_oauth_success", "OAuth successful."); if (strlen(access_params["oauth_token"]) && strlen(access_params["oauth_token_secret"])) {
} else { sendmsg(user, "_notice_oauth_success", "OAuth successful.");
sendmsg(user, "_error_oauth_token_access", "OAuth failed: could not get an access token."); return;
}
} }
sendmsg(user, "_error_oauth_token_access", "OAuth failed: could not get an access token.");
} }
void verified(string verifier) { void verified(string verifier) {

View File

@ -1,4 +1,5 @@
/* twitter client /* twitter client
* http://apiwiki.twitter.com/Twitter-API-Documentation
* *
* - register @ http://twitter.com/apps * - register @ http://twitter.com/apps
* - settings: * - settings:
@ -23,8 +24,10 @@ object load(object usr, string key, string secret, string request, string access
return ::load(usr, key, secret, request, access, authorize); return ::load(usr, key, secret, request, access, authorize);
} }
void parse_status_update(string body, string headers) { void parse_status_update(string body, string headers, int http_status) {
P3(("twitter/client:parse_status_update(%O, %O)\n", body, headers)) P3(("twitter/client:parse_status_update(%O, %O, %O)\n", body, headers, http_status))
if (http_status != R_OK)
sendmsg(user, "_error_twitter_status_update", "Error: failed to post status update on twitter.");
} }
void status_update(string text) { void status_update(string text) {
@ -36,8 +39,8 @@ void status_update(string text) {
fetch(ua, "http://api.twitter.com/1/statuses/update.json", "POST", (["status": text])); fetch(ua, "http://api.twitter.com/1/statuses/update.json", "POST", (["status": text]));
} }
void parse_home_timeline(string body, string headers) { void parse_home_timeline(string body, string headers, int http_status) {
P3(("twitter/client:parse_home_timeline(%O, %O)\n", body, headers)) P3(("twitter/client:parse_home_timeline(%O, %O, %O)\n", body, headers, http_status))
} }
void home_timeline() { void home_timeline() {