Permalink
Browse files

Add resolve example. One less TODO

  • Loading branch information...
1 parent 494d435 commit d1b518c2cb8ae2d12198e1b4011b5168105fd270 @azawawi committed Apr 2, 2016
Showing with 41 additions and 51 deletions.
  1. +31 −0 examples/02-native-api/resolve.pl6
  2. +0 −51 examples/TODO/resolve.c
  3. +10 −0 lib/Net/Curl/NativeCall.pm6
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+use lib 'lib';
+use NativeCall;
+use Net::Curl::NativeCall;
+
+my $res = CURLE_OK;
+
+#
+# Each single name resolve string should be written using the format
+# HOST:PORT:ADDRESS where HOST is the name libcurl will try to resolve,
+# PORT is the port number of the service where libcurl wants to connect to
+# the HOST and ADDRESS is the numerical IP address
+#
+my Pointer $host = curl_slist_append(Pointer.new(0), "example.com:80:127.0.0.1");
+
+my $curl = curl_easy_init;
+if $curl {
+ curl_easy_setopt($curl, CURLOPT_RESOLVE, $host);
+ curl_easy_setopt($curl, CURLOPT_URL, "http://example.com");
+ $res = curl_easy_perform($curl);
+
+ # always cleanup
+ curl_easy_cleanup($curl);
+}
+
+curl_slist_free_all($host);
+
+exit $res;
@@ -1,51 +0,0 @@
-/***************************************************************************
- * _ _ ____ _
- * Project ___| | | | _ \| |
- * / __| | | | |_) | |
- * | (__| |_| | _ <| |___
- * \___|\___/|_| \_\_____|
- *
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
- *
- * This software is licensed as described in the file COPYING, which
- * you should have received as part of this distribution. The terms
- * are also available at http://curl.haxx.se/docs/copyright.html.
- *
- * You may opt to use, copy, modify, merge, publish, distribute and/or sell
- * copies of the Software, and permit persons to whom the Software is
- * furnished to do so, under the terms of the COPYING file.
- *
- * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
- * KIND, either express or implied.
- *
- ***************************************************************************/
-#include <stdio.h>
-#include <curl/curl.h>
-
-int main(void)
-{
- CURL *curl;
- CURLcode res = CURLE_OK;
- struct curl_slist *host = NULL;
-
- /* Each single name resolve string should be written using the format
- HOST:PORT:ADDRESS where HOST is the name libcurl will try to resolve,
- PORT is the port number of the service where libcurl wants to connect to
- the HOST and ADDRESS is the numerical IP address
- */
- host = curl_slist_append(NULL, "example.com:80:127.0.0.1");
-
- curl = curl_easy_init();
- if(curl) {
- curl_easy_setopt(curl, CURLOPT_RESOLVE, host);
- curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
- res = curl_easy_perform(curl);
-
- /* always cleanup */
- curl_easy_cleanup(curl);
- }
-
- curl_slist_free_all(host);
-
- return (int)res;
-}
@@ -1360,6 +1360,12 @@ sub curl_easy_setopt_int32(Pointer $curl # Typedef<CURL>->
) is native(LIB) is symbol('curl_easy_setopt')
returns int32 { * }
+sub curl_easy_setopt_pointer(Pointer $curl # Typedef<CURL>->|void|*
+ ,int32 $option # CURLoption
+ ,Pointer $value
+ ) is native(LIB) is symbol('curl_easy_setopt')
+ returns int32 { * }
+
# TODO Waiting for multiple signatures
my sub curl_easy_setopt_cb(OpaquePointer, uint32, &cb (Pointer $ptr, uint32 $size, uint32 $nmemb, OpaquePointer $stream --> uint32))
returns uint32
@@ -1418,6 +1424,10 @@ multi sub curl_easy_setopt(Pointer $curl, Int $option, Int $value) is export {
return curl_easy_setopt_int32($curl, $option, $value);
}
+multi sub curl_easy_setopt(Pointer $curl, Int $option, Pointer $value) is export {
+ return curl_easy_setopt_pointer($curl, $option, $value);
+}
+
#-From /usr/include/curl/easy.h:30
#CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);
sub curl_easy_perform(Pointer $curl # Typedef<CURL>->|void|*

0 comments on commit d1b518c

Please sign in to comment.