static int append(void) { int i; CURLUcode rc; CURLU *urlp; int error = 0; for(i = 0; append_list[i].in && !error; i++) { urlp = curl_url(); if(!urlp) { error++; break; } rc = curl_url_set(urlp, CURLUPART_URL, append_list[i].in, append_list[i].urlflags); if(rc) error++; else rc = curl_url_set(urlp, CURLUPART_QUERY, append_list[i].q, append_list[i].qflags | CURLU_APPENDQUERY); if(error) ; else if(rc != append_list[i].ucode) { fprintf(stderr, "Append\nin: %s\nreturned %d (expected %d)\n", append_list[i].in, (int)rc, append_list[i].ucode); error++; } else if(append_list[i].ucode) { /* the expected error happened */ } else { char *url; rc = curl_url_get(urlp, CURLUPART_URL, &url, 0); if(rc) { fprintf(stderr, "%s:%d Get URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { if(checkurl(url, append_list[i].out)) { error++; } curl_free(url); } } curl_url_cleanup(urlp); } return error; }
int lcurl_url_create(lua_State *L, int error_mode){ lcurl_url_t *p; p = lutil_newudatap(L, lcurl_url_t, LCURL_URL); p->url = curl_url(); if(!p->url) return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, CURLUE_OUT_OF_MEMORY); p->err_mode = error_mode; if (lua_gettop(L) > 1) { const char *url = luaL_checkstring(L, 1); unsigned int flags = 0; CURLUcode code; if (lua_gettop(L) > 2) { flags = (unsigned int)lutil_optint64(L, 2, 0); } code = curl_url_set(p->url, CURLUPART_URL, url, flags); if (code != CURLUE_OK) { return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, code); } } return 1; }
static int get_parts(void) { int i; CURLUcode rc; CURLU *urlp; int error = 0; for(i = 0; get_parts_list[i].in && !error; i++) { urlp = curl_url(); if(!urlp) { error++; break; } rc = curl_url_set(urlp, CURLUPART_URL, get_parts_list[i].in, get_parts_list[i].urlflags); if(rc != get_parts_list[i].ucode) { fprintf(stderr, "Get parts\nin: %s\nreturned %d (expected %d)\n", get_parts_list[i].in, (int)rc, get_parts_list[i].ucode); error++; } else if(get_parts_list[i].ucode) { /* the expected error happened */ } else if(checkparts(urlp, get_parts_list[i].in, get_parts_list[i].out, get_parts_list[i].getflags)) error++; curl_url_cleanup(urlp); } return error; }
static int set_url(void) { int i; CURLUcode rc; CURLU *urlp; int error = 0; for(i = 0; set_url_list[i].in && !error; i++) { char *url = NULL; urlp = curl_url(); if(!urlp) break; rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].in, set_url_list[i].urlflags); if(!rc) { rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].set, set_url_list[i].setflags); if(rc) { fprintf(stderr, "%s:%d Set URL %s returned %d\n", __FILE__, __LINE__, set_url_list[i].set, (int)rc); error++; } else { rc = curl_url_get(urlp, CURLUPART_URL, &url, 0); if(rc) { fprintf(stderr, "%s:%d Get URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { if(checkurl(url, set_url_list[i].out)) { error++; } } } curl_free(url); } else if(rc != set_url_list[i].ucode) { fprintf(stderr, "Set URL\nin: %s\nreturned %d (expected %d)\n", set_url_list[i].in, (int)rc, set_url_list[i].ucode); error++; } curl_url_cleanup(urlp); } return error; }
int main(void) { CURLU *h; CURLUcode uc; char *host; char *path; h = curl_url(); /* get a handle to work with */ if(!h) return 1; /* parse a full URL */ uc = curl_url_set(h, CURLUPART_URL, "http://example.com/path/index.html", 0); if(uc) goto fail; /* extract host name from the parsed URL */ uc = curl_url_get(h, CURLUPART_HOST, &host, 0); if(!uc) { printf("Host name: %s\n", host); curl_free(host); } /* extract the path from the parsed URL */ uc = curl_url_get(h, CURLUPART_PATH, &path, 0); if(!uc) { printf("Path: %s\n", path); curl_free(path); } /* redirect with a relative URL */ uc = curl_url_set(h, CURLUPART_URL, "../another/second.html", 0); if(uc) goto fail; /* extract the new, updated path */ uc = curl_url_get(h, CURLUPART_PATH, &path, 0); if(!uc) { printf("Path: %s\n", path); curl_free(path); } fail: curl_url_cleanup(h); /* free url handle */ return 0; }
static CURLUcode updateurl(CURLU *u, const char *cmd, unsigned int setflags) { const char *p = cmd; CURLUcode uc; /* make sure the last command ends with a comma too! */ while(p) { char *e = strchr(p, ','); if(e) { size_t n = e-p; char buf[80]; char part[80]; char value[80]; memcpy(buf, p, n); buf[n] = 0; if(2 == sscanf(buf, "%79[^=]=%79[^,]", part, value)) { CURLUPart what = part2id(part); #if 0 /* for debugging this */ fprintf(stderr, "%s = %s [%d]\n", part, value, (int)what); #endif if(what > CURLUPART_ZONEID) fprintf(stderr, "UNKNOWN part '%s'\n", part); if(!strcmp("NULL", value)) uc = curl_url_set(u, what, NULL, setflags); else if(!strcmp("\"\"", value)) uc = curl_url_set(u, what, "", setflags); else uc = curl_url_set(u, what, value, setflags); if(uc) return uc; } p = e + 1; continue; } break; } return CURLUE_OK; }
static int set_parts(void) { int i; CURLUcode rc; int error = 0; for(i = 0; set_parts_list[i].set && !error; i++) { char *url = NULL; CURLU *urlp = curl_url(); if(!urlp) { error++; break; } if(set_parts_list[i].in) rc = curl_url_set(urlp, CURLUPART_URL, set_parts_list[i].in, set_parts_list[i].urlflags); else rc = CURLUE_OK; if(!rc) { CURLUcode uc = updateurl(urlp, set_parts_list[i].set, set_parts_list[i].setflags); if(uc != set_parts_list[i].pcode) { fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n", set_parts_list[i].set, (int)uc, set_parts_list[i].pcode); error++; } rc = curl_url_get(urlp, CURLUPART_URL, &url, 0); if(rc) { fprintf(stderr, "%s:%d Get URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else if(checkurl(url, set_parts_list[i].out)) { error++; } } else if(rc != set_parts_list[i].ucode) { fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n", set_parts_list[i].in, (int)rc, set_parts_list[i].ucode); error++; } curl_free(url); curl_url_cleanup(urlp); } return error; }
int test(char *URL) { CURL *handle = NULL; CURLcode res = 0; CURLU *urlp = NULL; CURLUcode uc = 0; global_init(CURL_GLOBAL_ALL); easy_init(handle); urlp = curl_url(); if(!urlp) { fprintf(stderr, "problem init URL api."); goto test_cleanup; } uc = curl_url_set(urlp, CURLUPART_URL, URL, 0); if(uc) { fprintf(stderr, "problem setting CURLUPART_URL."); goto test_cleanup; } /* demonstrate override behavior */ easy_setopt(handle, CURLOPT_URL, "http://www.example.com"); easy_setopt(handle, CURLOPT_CURLU, urlp); easy_setopt(handle, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(handle); if(res) { fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n", __FILE__, __LINE__, res, curl_easy_strerror(res)); goto test_cleanup; } test_cleanup: curl_url_cleanup(urlp); curl_easy_cleanup(handle); curl_global_cleanup(); return res; }
static int lcurl_url_set(lua_State *L, CURLUPart what){ lcurl_url_t *p = lcurl_geturl(L); CURLUcode code; const char *part; unsigned int flags = 0; luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING || lutil_is_null(L, 2), 2, "string expected"); part = lua_tostring(L, 2); flags = (unsigned int)lutil_optint64(L, 3, 0); code = curl_url_set(p->url, what, part, flags); if (code != CURLUE_OK) { return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, code); } lua_settop(L, 1); return 1; }
static int scopeid(void) { CURLU *u; int error = 0; CURLUcode rc; char *url; u = curl_url(); rc = curl_url_set(u, CURLUPART_URL, "https://[fe80::20c:29ff:fe9c:409b%25eth0]/hello.html", 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_set returned %d\n", __FILE__, __LINE__, (int)rc); error++; } rc = curl_url_get(u, CURLUPART_HOST, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } rc = curl_url_set(u, CURLUPART_HOST, "[::1]", 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d\n", __FILE__, __LINE__, (int)rc); error++; } rc = curl_url_get(u, CURLUPART_URL, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } rc = curl_url_set(u, CURLUPART_HOST, "example.com", 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d\n", __FILE__, __LINE__, (int)rc); error++; } rc = curl_url_get(u, CURLUPART_URL, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } rc = curl_url_set(u, CURLUPART_HOST, "[fe80::20c:29ff:fe9c:409b%25eth0]", 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d\n", __FILE__, __LINE__, (int)rc); error++; } rc = curl_url_get(u, CURLUPART_URL, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } rc = curl_url_get(u, CURLUPART_HOST, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } rc = curl_url_get(u, CURLUPART_ZONEID, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_ZONEID returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } rc = curl_url_set(u, CURLUPART_ZONEID, "clown", 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_set CURLUPART_ZONEID returned %d\n", __FILE__, __LINE__, (int)rc); error++; } rc = curl_url_get(u, CURLUPART_URL, &url, 0); if(rc != CURLUE_OK) { fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d\n", __FILE__, __LINE__, (int)rc); error++; } else { printf("we got %s\n", url); curl_free(url); } curl_url_cleanup(u); return error; }