uploaded liboauth for tizen_3.0
[platform/upstream/liboauth.git] / tests / oauthdatapost.c
1 /**
2  *  @brief experimental code to sign data uploads
3  *  @file oauthimageupload.c
4  *  @author Robin Gareus <robin@gareus.org>
5  *
6  * Copyright 2008, 2012 Robin Gareus <robin@gareus.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <oauth.h>
31
32 #ifdef USE_MMAP
33 #include <sys/mman.h>
34 #endif
35
36 /** 
37  * example oauth body signature and HTTP-POST.
38  * WARNING: <b> This is request type is not part of the 
39  * oauth core 1.0</b>. 
40  *
41  * This is an experimental extension.
42  */
43 int oauth_image_post(char *filename, char *url) {
44   const char *c_key         = "key"; //< consumer key
45   const char *c_secret      = "secret"; //< consumer secret
46   char *t_key               = NULL; //< access token key
47   char *t_secret            = NULL; //< access token secret
48
49   char *postarg = NULL;
50   char *req_url = NULL;
51   char *reply   = NULL;
52
53   char *filedata = NULL;
54   size_t filelen = 0;
55
56   FILE *F;
57
58   char *okey, *sign;
59   char *sig_url;
60
61   // get acces token - see oautexample.c
62   t_key    = strdup("key"); //< access token key
63   t_secret = strdup("secret"); //< access token secret
64
65   // read raw data to sign and send from file.
66   F= fopen(filename, "r");
67   if (!F) return 1;
68   fseek(F, 0L, SEEK_END);
69   filelen= ftell(F);
70   rewind(F);
71
72   #ifdef USE_MMAP
73   filedata=mmap(NULL,filelen,PROT_READ,MAP_SHARED,fileno(F),0L);
74   #else 
75   filedata=malloc(filelen*sizeof(char));
76   if (filelen != fread(filedata,sizeof(char), filelen, F)) {
77         fclose(F);
78         return 2;
79   }
80   fclose(F);
81   #endif
82
83   // sign the body
84   okey = oauth_catenc(2, c_secret, t_secret);
85   sign = oauth_sign_hmac_sha1_raw(filedata,filelen,okey,strlen(okey));
86   free(okey);
87   sig_url = malloc(63+strlen(url)+strlen(sign));
88   sprintf(sig_url,"%s&xoauth_body_signature=%s&xoauth_body_signature_method=HMAC_SHA1",url, sign);
89
90   // sign a POST request
91   req_url = oauth_sign_url2(sig_url, &postarg, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
92   free(sig_url);
93
94   // append the oauth [post] parameters to the request-URL!!
95   sig_url = malloc(2+strlen(req_url)+strlen(postarg));
96   sprintf(sig_url,"%s?%s",req_url, postarg);
97
98   printf("POST:'%s'\n",sig_url);
99   //reply = oauth_post_file(sig_url,filename,filelen,"Content-Type: image/jpeg;");
100   printf("reply:'%s'\n",reply);
101
102   if(req_url) free(req_url);
103   if(postarg) free(postarg);
104   if(reply) free(reply);
105   if(t_key) free(t_key);
106   if(t_secret) free(t_secret);
107
108   #ifdef USE_MMAP
109   munmap(filedata,filelen);
110   fclose(F);
111   #else 
112   if(filedata) free(filedata);
113   #endif
114   return(0);
115 }
116
117
118 /**
119  * Main Test and Example Code.
120  * 
121  * compile:
122  *  gcc -lssl -loauth -o oauthdatapost oauthdatapost.c
123  */
124
125 int main (int argc, char **argv) {
126   char *base_url = "http://mms06.test.mediamatic.nl";
127   char *filename = "/tmp/test.jpg";
128   int anyid = 18704;
129   char *title = "test";
130   char *url;
131
132   if (argc>4) base_url = argv[4];
133   if (argc>3) title = argv[3];
134   if (argc>2) anyid = atoi(argv[2]);
135   if (argc>1) filename = argv[1];
136
137   // TODO check if file exists; also read oauth-params from args or file
138
139   // anyMeta.nl image-post module URL
140   url = malloc(1024*sizeof(char));
141   if (anyid<1 && !title) 
142     sprintf(url,"%s/module/ImagePost/",base_url);
143   else if (anyid>0 && !title) 
144     sprintf(url,"%s/module/ImagePost/%i?echoid=1",base_url,anyid);
145   else if (anyid<1 && title) {
146     char *tp = oauth_url_escape(title);
147     sprintf(url,"%s/module/ImagePost/?title=%s",base_url,tp);
148     free(tp);
149   }
150   else if (anyid>0 && title) {
151     char *tp = oauth_url_escape(title);
152     sprintf(url,"%s/module/ImagePost/%i?echoid=1&title=%s",base_url,anyid,tp);
153     free(tp);
154   }
155
156   // doit
157   switch(oauth_image_post(filename, url)) {
158     case 0:
159       printf("request ok.\n");
160       break;
161     default:
162       printf("upload failed.\n");
163       break;
164   }
165   return(0);
166 }