Advertisement
MdSadmanSiraj

requestor_hw_mssiraj.c

Jul 19th, 2022 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 KB | None | 0 0
  1. /*
  2. COMPILATION COMMAND:
  3. /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
  4.  
  5. COMPILATION NOTES:
  6. 1. Please replace "/home/mssiraj/buildroot-2022.02.3/" with the path to your own buildroot directory.
  7.  
  8. 2. This program uses the "argp" library which requires the following configuration in buildroot:
  9. make nconfig -> Target packages -> Libraries -> Other -> argp-standalone
  10. */
  11.  
  12. /*
  13. USAGE EXAMPLES:
  14. requestor_hw_mssiraj --get --url=http://eu.httpbin.org/anything
  15. requestor_hw_mssiraj --post --url=http://eu.httpbin.org/anything "name=sadman&uni=UNM"
  16. requestor_hw_mssiraj --put --url=http://eu.httpbin.org/anything "project=libcurl HTTP"
  17. requestor_hw_mssiraj --delete --url=http://eu.httpbin.org/anything "name=sadman&uni=UNM"
  18. requestor_hw_mssiraj -?/--help or --usage
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <curl/curl.h>
  23. #include <stdlib.h>
  24. #include <argp.h>
  25.  
  26. #define OK 0
  27. #define INIT_ERR 1
  28. #define REQ_ERR 2
  29.  
  30. #define URL "http://localhost"
  31.  
  32. /* Program documentation. */
  33. static char doc[] = "requestor_hw -- a program that uses libcurl to communicate via HTTP.";
  34.  
  35. /* A description of the arguments we accept. */
  36. static char args_doc[] = "STRING (Please put this string value within single or double quotes). If not set, default string value will be used.";
  37.  
  38. /* Program options. */
  39. static struct argp_option options[] = {
  40.   {"get", 'g', 0, 0, "HTTP GET Request" },
  41.   {"post", 'o', 0, 0, "HTTP POST Request" },
  42.   {"put", 'p', 0, 0, "HTTP PUT Request" },
  43.   {"delete", 'd', 0, 0, "HTTP DELETE Request" },
  44.   {"url", 'u', "URL", 0, "Set URL. Otherwise, default URL: http://localhost will be set." },
  45.   { 0 }
  46. };
  47.  
  48. /* Used by main to communicate with parse_opt. */
  49. struct arguments
  50. {
  51.   char *args;
  52.   int http_get;
  53.   int http_post;
  54.   int http_put;
  55.   int http_delete;
  56.   int user_url;
  57.   char *url;
  58.   // char *text;
  59. };
  60.  
  61. /* Parse a single option. */
  62. static error_t
  63. parse_opt (int key, char *arg, struct argp_state *state)
  64. {
  65.   /* Get the input argument from argp_parse, which we
  66.      know is a pointer to our arguments structure. */
  67.   struct arguments *arguments = state->input;
  68.  
  69.   switch (key)
  70.     {
  71.     case 'g':
  72.       printf("\nPerforming HTTP GET Request...\n");
  73.       arguments->http_get = 1;
  74.       break;
  75.     case 'o':
  76.       printf("\nPerforming HTTP POST Request...\n");
  77.       arguments->http_post = 1;
  78.       break;
  79.     case 'p':
  80.       printf("\nPerforming HTTP PUT Request...\n");
  81.       arguments->http_put = 1;
  82.       break;
  83.     case 'd':
  84.       printf("\nPerforming HTTP DELETE Request...\n");
  85.       arguments->http_delete = 1;
  86.       break;
  87.     case 'u':
  88.       arguments->user_url = 1;
  89.       arguments->url = arg;
  90.       // printf(arguments.url);
  91.       break;
  92.  
  93.     case ARGP_KEY_ARG:
  94.       //if (state->arg_num >= 1)
  95.         /* Too many arguments. */
  96.         //argp_usage (state);
  97.       arguments->args = arg;
  98.       break;
  99.  
  100.     //case ARGP_KEY_END:
  101.       //if (state->arg_num < 1)
  102.         /* Not enough arguments. */
  103.         //argp_usage (state);
  104.       //break;
  105.  
  106.     default:
  107.       return ARGP_ERR_UNKNOWN;
  108.     }
  109.   return 0;
  110. }
  111.  
  112. /* Our argp parser. */
  113. static struct argp argp = { options, parse_opt, args_doc, doc };
  114.  
  115. int main(int argc, char **argv) {
  116.  
  117.     struct arguments arguments;
  118.  
  119.     /* Default values. */
  120.     arguments.http_get = 0;
  121.     arguments.http_post = 0;
  122.     arguments.http_put = 0;
  123.     arguments.http_delete = 0;
  124.     arguments.user_url = 0;
  125.     arguments.url = "http://localhost";
  126.     arguments.args = "INPUT=Default STRING Value";
  127.    
  128.     /* Parse our arguments; every option seen by parse_opt will
  129.         be reflected in arguments. */
  130.     argp_parse(&argp, argc, argv, 0, 0, &arguments);
  131.    
  132.     if (arguments.user_url) {
  133.         printf("\nGiven URL = %s\n", arguments.url);
  134.     } else {
  135.         printf("\nGiven URL = %s (default URL)\n", arguments.url);
  136.     }
  137.     printf("\nGiven STRING = %s\n", arguments.args);
  138.    
  139.     // No HTTP Request
  140.         if (arguments.http_get == 0 &&
  141.             arguments.http_post == 0 &&
  142.             arguments.http_put == 0 &&
  143.             arguments.http_delete == 0) {
  144.             printf("\nNo HTTP Request received from User.\nUse the option -?/--help or --usage for more information.\n");
  145.             }
  146.  
  147.    
  148.     CURL *curl;
  149.     CURLcode res;
  150.     curl = curl_easy_init();
  151.    
  152.     if (curl) {
  153.         curl_easy_setopt(curl, CURLOPT_URL, arguments.url);
  154.         // HTTP GET
  155.         if (arguments.http_get) {
  156.             curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  157.             printf("\nHTTP GET Response:\n");
  158.             res = curl_easy_perform(curl);
  159.             }
  160.         // HTTP POST   
  161.         if (arguments.http_post) {
  162.             curl_easy_setopt(curl, CURLOPT_POST, 1L);
  163.             curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.args);
  164.             printf("\nHTTP POST Response:\n");
  165.             res = curl_easy_perform(curl);
  166.             }
  167.         // HTTP PUT
  168.         if (arguments.http_put) {
  169.             curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
  170.             curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.args);
  171.             printf("\nHTTP PUT Response:\n");
  172.             res = curl_easy_perform(curl);
  173.             }
  174.         // HTTP DELETE 
  175.         if (arguments.http_delete) {
  176.             curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  177.             curl_easy_setopt(curl, CURLOPT_POSTFIELDS, arguments.args);
  178.             printf("\nHTTP DELETE Response:\n");
  179.             res = curl_easy_perform(curl);
  180.             }
  181.         if (res != CURLE_OK) {
  182.             return REQ_ERR;
  183.             }
  184.         curl_easy_cleanup(curl);
  185.         } else {
  186.             return INIT_ERR;
  187.             }
  188.  
  189.     return OK;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement