Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- COMPILATION COMMAND:
- /home/mssiraj/buildroot-2022.02.3/output/host/usr/bin/arm-linux-gcc --sysroot=/home/mssiraj/buildroot-2022.02.3/output/staging requestor_hw_mssiraj.c -o requestor_hw_mssiraj -lcurl -uClibc -lc -largp
- COMPILATION NOTES:
- 1. Please replace "/home/mssiraj/buildroot-2022.02.3/" with the path to your own buildroot directory.
- 2. This program uses the "argp" library which requires the following configuration in buildroot:
- make nconfig -> Target packages -> Libraries -> Other -> argp-standalone
- */
- /*
- USAGE EXAMPLES:
- requestor_hw_mssiraj --get --url=http://eu.httpbin.org/anything
- requestor_hw_mssiraj --post --url=http://eu.httpbin.org/anything "name=sadman&uni=UNM"
- requestor_hw_mssiraj --put --url=http://eu.httpbin.org/anything "project=libcurl HTTP"
- requestor_hw_mssiraj --delete --url=http://eu.httpbin.org/anything "name=sadman&uni=UNM"
- requestor_hw_mssiraj -?/--help or --usage
- */
- #include <stdio.h>
- #include <curl/curl.h>
- #include <stdlib.h>
- #include <argp.h>
- #define OK 0
- #define INIT_ERR 1
- #define REQ_ERR 2
- #define URL "http://localhost"
- /* Program documentation. */
- static char doc[] = "requestor_hw -- a program that uses libcurl to communicate via HTTP.";
- /* A description of the arguments we accept. */
- static char args_doc[] = "STRING (Please put this string value within single or double quotes). If not set, default string value will be used.";
- /* Program options. */
- static struct argp_option options[] = {
- {"get", 'g', 0, 0, "HTTP GET Request" },
- {"post", 'o', 0, 0, "HTTP POST Request" },
- {"put", 'p', 0, 0, "HTTP PUT Request" },
- {"delete", 'd', 0, 0, "HTTP DELETE Request" },
- {"url", 'u', "URL", 0, "Set URL. Otherwise, default URL: http://localhost will be set." },
- { 0 }
- };
- /* Used by main to communicate with parse_opt. */
- struct arguments
- {
- char *args;
- int http_get;
- int http_post;
- int http_put;
- int http_delete;
- int user_url;
- char *url;
- // char *text;
- };
- /* Parse a single option. */
- static error_t
- parse_opt (int key, char *arg, struct argp_state *state)
- {
- /* Get the input argument from argp_parse, which we
- know is a pointer to our arguments structure. */
- struct arguments *arguments = state->input;
- switch (key)
- {
- case 'g':
- printf("\nPerforming HTTP GET Request...\n");
- arguments->http_get = 1;
- break;
- case 'o':
- printf("\nPerforming HTTP POST Request...\n");
- arguments->http_post = 1;
- break;
- case 'p':
- printf("\nPerforming HTTP PUT Request...\n");
- arguments->http_put = 1;
- break;
- case 'd':
- printf("\nPerforming HTTP DELETE Request...\n");
- arguments->http_delete = 1;
- break;
- case 'u':
- arguments->user_url = 1;
- arguments->url = arg;
- // printf(arguments.url);
- break;
- case ARGP_KEY_ARG:
- //if (state->arg_num >= 1)
- /* Too many arguments. */
- //argp_usage (state);
- arguments->args = arg;
- break;
- //case ARGP_KEY_END:
- //if (state->arg_num < 1)
- /* Not enough arguments. */
- //argp_usage (state);
- //break;
- default:
- return ARGP_ERR_UNKNOWN;
- }
- return 0;
- }
- /* Our argp parser. */
- static struct argp argp = { options, parse_opt, args_doc, doc };
- int main(int argc, char **argv) {
- struct arguments arguments;
- /* Default values. */
- arguments.http_get = 0;
- arguments.http_post = 0;
- arguments.http_put = 0;
- arguments.http_delete = 0;
- arguments.user_url = 0;
- arguments.url = "http://localhost";
- arguments.args = "INPUT=Default STRING Value";
- /* Parse our arguments; every option seen by parse_opt will
- be reflected in arguments. */
- argp_parse(&argp, argc, argv, 0, 0, &arguments);
- if (arguments.user_url) {
- printf("\nGiven URL = %s\n", arguments.url);
- } else {
- printf("\nGiven URL = %s (default URL)\n", arguments.url);
- }
- printf("\nGiven STRING = %s\n", arguments.args);
- // No HTTP Request
- if (arguments.http_get == 0 &&
- arguments.http_post == 0 &&
- arguments.http_put == 0 &&
- arguments.http_delete == 0) {
- printf("\nNo HTTP Request received from User.\nUse the option -?/--help or --usage for more information.\n");
- }
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if (curl) {
- curl_easy_setopt(curl, CURLOPT_URL, arguments.url);
- // HTTP GET
- if (arguments.http_get) {
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
- printf("\nHTTP GET Response:\n");
- res = curl_easy_perform(curl);
- }
- // HTTP POST
- if (arguments.http_post) {
- curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.args);
- printf("\nHTTP POST Response:\n");
- res = curl_easy_perform(curl);
- }
- // HTTP PUT
- if (arguments.http_put) {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.args);
- printf("\nHTTP PUT Response:\n");
- res = curl_easy_perform(curl);
- }
- // HTTP DELETE
- if (arguments.http_delete) {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.args);
- printf("\nHTTP DELETE Response:\n");
- res = curl_easy_perform(curl);
- }
- if (res != CURLE_OK) {
- return REQ_ERR;
- }
- curl_easy_cleanup(curl);
- } else {
- return INIT_ERR;
- }
- return OK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement