mirror of
https://github.com/rkd77/elinks.git
synced 2024-11-04 08:17:17 -05:00
[spidermonkey] ajax sync GET with curl
This commit is contained in:
parent
2e33d2c1ca
commit
704229014c
@ -51,6 +51,8 @@
|
|||||||
#include "viewer/text/link.h"
|
#include "viewer/text/link.h"
|
||||||
#include "viewer/text/vs.h"
|
#include "viewer/text/vs.h"
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -928,6 +930,29 @@ xhr_loading_callback(struct download *download, struct xhr *xhr)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
write_data(void *ptr, size_t size, size_t nmemb, void *stream)
|
||||||
|
{
|
||||||
|
struct xhr *xhr = (struct xhr *)stream;
|
||||||
|
|
||||||
|
size_t length = 0;
|
||||||
|
|
||||||
|
if (xhr->responseText) {
|
||||||
|
length = strlen(xhr->responseText);
|
||||||
|
}
|
||||||
|
char *n = (char *)mem_realloc(xhr->responseText, length + size * nmemb + 1);
|
||||||
|
|
||||||
|
if (n) {
|
||||||
|
xhr->responseText = n;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
memcpy(xhr->responseText + length, ptr, (size * nmemb));
|
||||||
|
xhr->responseText[length + size * nmemb] = '\0';
|
||||||
|
|
||||||
|
return nmemb;
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
xhr_send(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
xhr_send(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
||||||
{
|
{
|
||||||
@ -1002,6 +1027,31 @@ xhr_send(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
|||||||
args.rval().setUndefined();
|
args.rval().setUndefined();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!xhr->async) {
|
||||||
|
char *url = get_uri_string(xhr->uri, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD);
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
args.rval().setUndefined();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr->isSend = true;
|
||||||
|
CURL *curl_handle = curl_easy_init();
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L);
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, xhr);
|
||||||
|
curl_easy_perform(curl_handle);
|
||||||
|
curl_easy_cleanup(curl_handle);
|
||||||
|
xhr->readyState = DONE;
|
||||||
|
xhr->status = 200;
|
||||||
|
mem_free(url);
|
||||||
|
|
||||||
|
args.rval().setUndefined();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
xhr->download.data = xhr;
|
xhr->download.data = xhr;
|
||||||
xhr->download.callback = (download_callback_T *)xhr_loading_callback;
|
xhr->download.callback = (download_callback_T *)xhr_loading_callback;
|
||||||
load_uri(xhr->uri, doc_view->session->referrer, &xhr->download, PRI_MAIN, CACHE_MODE_NORMAL, -1);
|
load_uri(xhr->uri, doc_view->session->referrer, &xhr->download, PRI_MAIN, CACHE_MODE_NORMAL, -1);
|
||||||
@ -1513,11 +1563,7 @@ xhr_get_property_responseText(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|||||||
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
||||||
struct xhr *xhr = JS::GetMaybePtrFromReservedSlot<struct xhr>(hobj, 0);
|
struct xhr *xhr = JS::GetMaybePtrFromReservedSlot<struct xhr>(hobj, 0);
|
||||||
|
|
||||||
if (!xhr || !xhr->responseType) {
|
if (!xhr) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(strlen(xhr->responseType) == 0 || !strcasecmp(xhr->responseType, "text"))) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user