CURL sample, copyright update

This commit is contained in:
Nikolay Igotti
2017-03-30 16:23:16 +03:00
parent 10a3e3d0e1
commit 61728ab3fd
13 changed files with 218 additions and 2 deletions
+14
View File
@@ -1,4 +1,18 @@
#!/usr/bin/env bash
#
# Copyright 2010-2017 JetBrains s.r.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ -z "$JAVACMD" -a -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then
JAVACMD="$JAVA_HOME/bin/java"
+14
View File
@@ -1,4 +1,18 @@
#!/usr/bin/env bash
#
# Copyright 2010-2017 JetBrains s.r.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ -z "$JAVACMD" -a -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then
JAVACMD="$JAVA_HOME/bin/java"
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# Copyright 2010-2015 JetBrains s.r.o.
# Copyright 2010-2017 JetBrains s.r.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# Copyright 2010-2015 JetBrains s.r.o.
# Copyright 2010-2017 JetBrains s.r.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
+10
View File
@@ -0,0 +1,10 @@
# HTTP client
This example shows how to communicate with libcurl, HTTP/HTTPS/FTP/etc client library.
To build use `./build.sh` script without arguments (or specify `TARGET` variable if cross-compiling).
To run use
./Curl.kexe https://jetbrains.com
It will perform HTTP get and print out the data obtained.
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
PATH=../../dist/bin:../../bin:$PATH
DIR=.
CFLAGS_macbook=-I/opt/local/include
CFLAGS_linux=-I/usr/include
LINKER_ARGS_macbook="-L/opt/local/lib -lcurl"
LINKER_ARGS_linux="-L/usr/lib/x86_64-linux-gnu -lcurl"
if [ x$TARGET == x ]; then
case "$OSTYPE" in
darwin*) TARGET=macbook ;;
linux*) TARGET=linux ;;
*) echo "unknown: $OSTYPE" && exit 1;;
esac
fi
var=CFLAGS_${TARGET}
CFLAGS=${!var}
var=LINKER_ARGS_${TARGET}
LINKER_ARGS=${!var}
var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build.
interop -copt:"$CFLAGS" -copt:-I. -def:$DIR/libcurl.def -target:$TARGET || exit 1
konanc -target $TARGET src libcurl -nativelibrary libcurlstubs.bc -linkerArgs "$LINKER_ARGS" -o Curl.kexe || exit 1
+8
View File
@@ -0,0 +1,8 @@
#include <stdio.h>
// Those types are needed but not defined or used in libcurl headers.
typedef size_t header_callback(char *buffer, size_t size, size_t nitems, void *userdata);
typedef size_t write_data_callback(void *buffer, size_t size, size_t nmemb, void *userp);
extern void something_using_callback1(header_callback*);
extern void something_using_callback2(write_data_callback*);
+2
View File
@@ -0,0 +1,2 @@
headers = curl/curl.h callbacks.h
excludedFunctions = zopen pfctlinput
+14
View File
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/libs/libcurl" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="module" module-name="konan" />
</component>
</module>
+3
View File
@@ -0,0 +1,3 @@
fun main(args: Array<String>) {
org.konan.libcurl.main(args)
}
+72
View File
@@ -0,0 +1,72 @@
package org.konan.libcurl
import kotlinx.cinterop.*
import libcurl.*
class CUrl(val url: String) {
val repositoryIndex = repository.run {
val index = size
add(this@CUrl)
index
}
val curl = curl_easy_init();
init {
curl_easy_setopt(curl, CURLOPT_URL, url)
val header: CPointer<header_callback> = staticCFunction(::header_callback)
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header)
curl_easy_setopt(curl, CURLOPT_HEADERDATA, interpretCPointer<CPointed>(nativeNullPtr + 1 + repositoryIndex.toLong()))
}
val header = Event<String>()
val data = Event<String>()
fun fetch() {
val res = curl_easy_perform(curl)
if (res != CURLE_OK)
println("curl_easy_perform() failed: ${curl_easy_strerror(res)}")
}
fun close() {
curl_easy_cleanup(curl)
// Cannot remove urls from repository because it would change other indices
// We could potentially remove it and update all indices in tails
repository[repositoryIndex] = null
}
companion object {
val repository = ArrayList<CUrl?>()
}
}
fun CPointer<CInt8Var>.toKString(length: Int): String {
val bytes = ByteArray(length)
nativeMemUtils.getByteArray(pointed, bytes, length)
return kotlin.text.fromUtf8Array(bytes, 0, bytes.size)
}
fun header_callback(buffer: CPointer<CInt8Var>?, size: size_t, nitems: size_t, userdata: COpaquePointer?): size_t {
if (buffer == null) return 0
val header = buffer.toKString((size * nitems).toInt()).trim()
if (userdata != null) {
val id = userdata.rawValue.toLong() - 1
val curl = CUrl.repository[id.toInt()]
if (curl != null)
curl.header(header)
}
return size * nitems
}
/*
fun write_callback(buffer: COpaquePointer?, size: size_t, nitems: size_t, userdata: COpaquePointer?): size_t {
if (buffer == null) return 0
if (userdata != null) {
val id = userdata.rawValue.toLong() - 1
val curl = CUrl.repository[id.toInt()]
if (curl != null)
curl.data(buffer.)
}
return size * nitems
}
*/
+29
View File
@@ -0,0 +1,29 @@
package org.konan.libcurl
typealias EventHandler<T> = (T) -> Unit
class Event<T : Any> {
private var handlers = emptyList<EventHandler<T>>()
fun subscribe(handler: EventHandler<T>) {
handlers += handler
}
fun unsubscribe(handler: EventHandler<T>) {
handlers -= handler
}
operator fun plusAssign(handler: EventHandler<T>) = subscribe(handler)
operator fun minusAssign(handler: EventHandler<T>) = unsubscribe(handler)
operator fun invoke(value: T) {
var exception: Throwable? = null
for (handler in handlers) {
try {
handler(value)
} catch (e: Throwable) {
exception = e
}
}
exception?.let { throw it }
}
}
+23
View File
@@ -0,0 +1,23 @@
package org.konan.libcurl
fun main(args: Array<String>) {
if (args.size == 0)
return help()
val curl = CUrl(args[0])
curl.header += {
println("[H] $it")
}
curl.fetch()
curl.close()
/*
val write_data: CPointer<write_data_callback> = staticCFunction(::write_data)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
*/
}
fun help() {
println("ERROR: missing URL command line argument")
}