Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created July 14, 2015 21:14
Show Gist options
  • Save p120ph37/71fb340f7a0d38fa5443 to your computer and use it in GitHub Desktop.
Save p120ph37/71fb340f7a0d38fa5443 to your computer and use it in GitHub Desktop.
libcurl basic-auth post example
/*
* HTTP POST with authentiction using "basic" method.
* Hybrid of anyauthput.c and postinmemory.c
*
* Usage:
* cc basicauthpost.c -lcurl -o basicauthpost
* ./basicauthpost
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *context) {
size_t bytec = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)context;
mem->memory = realloc(mem->memory, mem->size + bytec + 1);
if(mem->memory == NULL) {
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), ptr, bytec);
mem->size += bytec;
mem->memory[mem->size] = 0;
return nmemb;
}
int main(void) {
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
chunk.memory[chunk.size] = 0;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://ameriwether.com/cgi-bin/info.pl");
char *val1 = curl_easy_escape(curl, "tricky & ugly", 0);
char *val2 = curl_easy_escape(curl, "Hello from cURL!!!", 0);
char *fields = malloc(4 + strlen(val1) + 1 + 4 + strlen(val2) + 1);
sprintf(fields, "foo=%s&bar=%s", val1, val2);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
// The output from the example URL is easier to read as plain text.
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: text/plain");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Make the example URL work even if your CA bundle is missing.
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
} else {
printf("%s\n",chunk.memory);
}
// Remember to call the appropriate "free" functions.
free(fields);
curl_free(val1);
curl_free(val2);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
free(chunk.memory);
curl_global_cleanup();
}
return 0;
}
@p120ph37
Copy link
Author

This is what it looks like when run:

$ ./basicauthpost
TCP Connection:
 50.116.0.76:60999 > 50.116.0.76:443
SSL Layer (TLSv1.2):
 CIPHER=DHE-RSA-AES128-SHA
 CIPHER_ALGKEYSIZE=128
 CIPHER_EXPORT=false
 CIPHER_USEKEYSIZE=128
 CLIENT_VERIFY=NONE
 COMPRESS_METHOD=NULL
 SECURE_RENEG=true
 SESSION_ID=15630EAD7B82D5FA7BB74AF35869DF195AB2B307F5C9C34A84BC5E9EFAA6A9B5
 TLS_SNI=ameriwether.com
HTTP Request:
 POST /cgi-bin/info.pl HTTP/1.1
HTTP Basic Auth:
 Username: user
 Password: password
HTTP Headers:
 Accept: text/plain
 ContentLength: 56
 ContentType: application/x-www-form-urlencoded
 Host: ameriwether.com
CGI Params:
 foo=tricky & ugly
 bar=Hello from cURL!!!

The source for the CGI that is generating this response to the POST can be found here: https://gist.github.com/p120ph37/3bf6c500e0f00ab635da

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment