Fork of OAuth4Tw to use mbed's official EthernetInterface and HTTPClient

Fork of OAuth4Tw by Masayoshi Takahashi

Committer:
vpcola
Date:
Thu Apr 30 10:19:31 2015 +0000
Revision:
1:b3501683b856
Parent:
0:0048b264a3ad
Fork of OAuth4Tw to use normal EthernetInterface and HTTPClient library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takahashim 0:0048b264a3ad 1 /*
takahashim 0:0048b264a3ad 2 * OAuth http functions in POSIX-C.
takahashim 0:0048b264a3ad 3 *
takahashim 0:0048b264a3ad 4 * Copyright 2007, 2008, 2009, 2010 Robin Gareus <robin@gareus.org>
takahashim 0:0048b264a3ad 5 *
takahashim 0:0048b264a3ad 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
takahashim 0:0048b264a3ad 7 * of this software and associated documentation files (the "Software"), to deal
takahashim 0:0048b264a3ad 8 * in the Software without restriction, including without limitation the rights
takahashim 0:0048b264a3ad 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
takahashim 0:0048b264a3ad 10 * copies of the Software, and to permit persons to whom the Software is
takahashim 0:0048b264a3ad 11 * furnished to do so, subject to the following conditions:
takahashim 0:0048b264a3ad 12 *
takahashim 0:0048b264a3ad 13 * The above copyright notice and this permission notice shall be included in
takahashim 0:0048b264a3ad 14 * all copies or substantial portions of the Software.
takahashim 0:0048b264a3ad 15 *
takahashim 0:0048b264a3ad 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
takahashim 0:0048b264a3ad 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
takahashim 0:0048b264a3ad 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
takahashim 0:0048b264a3ad 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
takahashim 0:0048b264a3ad 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
takahashim 0:0048b264a3ad 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
takahashim 0:0048b264a3ad 22 * THE SOFTWARE.
takahashim 0:0048b264a3ad 23 *
takahashim 0:0048b264a3ad 24 */
takahashim 0:0048b264a3ad 25 #if HAVE_CONFIG_H
takahashim 0:0048b264a3ad 26 # include <config.h>
takahashim 0:0048b264a3ad 27 #endif
takahashim 0:0048b264a3ad 28
takahashim 0:0048b264a3ad 29 #include <stdio.h>
takahashim 0:0048b264a3ad 30 #include <stdarg.h>
takahashim 0:0048b264a3ad 31 #include <stdlib.h>
takahashim 0:0048b264a3ad 32 #include <string.h>
takahashim 0:0048b264a3ad 33
takahashim 0:0048b264a3ad 34 #include "oauth.h"
takahashim 0:0048b264a3ad 35
takahashim 0:0048b264a3ad 36 #include <HTTPClient.h>
vpcola 1:b3501683b856 37 #include "oauth_data.h"
vpcola 1:b3501683b856 38
vpcola 1:b3501683b856 39 #define RESULT_BUFFER_SIZ 1024
vpcola 1:b3501683b856 40 static char result_buffer[RESULT_BUFFER_SIZ];
takahashim 0:0048b264a3ad 41
takahashim 0:0048b264a3ad 42 /* wrapper functions */
takahashim 0:0048b264a3ad 43
takahashim 0:0048b264a3ad 44 /**
takahashim 0:0048b264a3ad 45 * do a HTTP POST request, wait for it to finish
takahashim 0:0048b264a3ad 46 * and return the content of the reply.
takahashim 0:0048b264a3ad 47 * (requires libcurl or a command-line HTTP client)
takahashim 0:0048b264a3ad 48 *
takahashim 0:0048b264a3ad 49 * more documentation in oauth.h
takahashim 0:0048b264a3ad 50 *
takahashim 0:0048b264a3ad 51 * @param u url to query
takahashim 0:0048b264a3ad 52 * @param p postargs to send along with the HTTP request.
takahashim 0:0048b264a3ad 53 * @return In case of an error NULL is returned; otherwise a pointer to the
takahashim 0:0048b264a3ad 54 * replied content from HTTP server. latter needs to be freed by caller.
takahashim 0:0048b264a3ad 55 */
takahashim 0:0048b264a3ad 56 std::string oauth_http_post(const char *u, const char *p)
takahashim 0:0048b264a3ad 57 {
takahashim 0:0048b264a3ad 58 HTTPClient http;
vpcola 1:b3501683b856 59 OAuthDataOut req("application/x-www-form-urlencoded", p);
vpcola 1:b3501683b856 60 OAuthDataIn res(result_buffer, RESULT_BUFFER_SIZ);
vpcola 1:b3501683b856 61
takahashim 0:0048b264a3ad 62 http.post(u, req, &res);
vpcola 1:b3501683b856 63 return res.getData();
takahashim 0:0048b264a3ad 64 }
takahashim 0:0048b264a3ad 65