how to store ld+json data to cayley - json-ld

when i use cayley api/v2/write interface to store json-ld data, i got some error.
cayley version:
➜ cayley ./cayley version
Cayley v0.6.1 built 2017-04-12T11:41:20-07:00
test command:
curl -H "Content-Type:application/ld+json" -X POST -d '[{"#context":"http://json-ld.org/contexts/persion.jsonld","#id":"http://dbpedia.org/resource/John_Lennon","name":"John Lennon","born":"1940-10-09","spouse":"http://dbpedia.org/resource/Cynthia_Lennon"}]' http://localhost:64210/api/v2/write
Error Info:
2017/06/08 08:04:37 http: panic serving 127.0.0.1:52187: runtime error: invalid memory address or nil pointer dereference
goroutine 13 [running]:
net/http.(*conn).serve.func1(0xc42021f180)
/usr/lib/go/src/net/http/server.go:1721 +0xd0
panic(0x190fbc0, 0x2062b80)
/usr/lib/go/src/runtime/panic.go:489 +0x2cf
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.(*DocumentLoader).loadDocument(0x0, 0xc420215ec0, 0x2a, 0x0, 0x1, 0xc4202bd120)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/documentLoader.go:27 +0xd0
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.parse(0xc4201112c8, 0x18cb700, 0xc4202bd040, 0xc4202bd120, 0x1, 0x1, 0x0, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/contextApi.go:58 +0x1293
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.expand(0xc4201112c8, 0x0, 0x190b4a0, 0xc420215e90, 0x0, 0x0, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/expansionApi.go:55 +0x5876
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.expand(0xc4201112c8, 0x0, 0x18bf120, 0xc4202beae0, 0xc42032c000, 0x0, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/expansionApi.go:27 +0x59d6
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.Expand(0x18bf120, 0xc4202beae0, 0xc4201df800, 0x0, 0xc420276000, 0x2258000, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/api.go:54 +0x360
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.toRDF(0xc420111898, 0x18bf120, 0xc4202beae0, 0xc4202bd0c0, 0xc420215f80, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/serializationApi.go:11 +0x6c
github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld.ToRDF(0x18bf120, 0xc4202beae0, 0xc4201df800, 0xc42009e8e8, 0x18aa660, 0xc4202bcff0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/linkeddata/gojsonld/api.go:183 +0x19e
github.com/cayleygraph/cayley/quad/jsonld.NewReaderFromMap(0x18bf120, 0xc4202beae0, 0xc4202bcff0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/quad/jsonld/jsonld.go:39 +0x79
github.com/cayleygraph/cayley/quad/jsonld.NewReader(0x2340380, 0xc420231380, 0x2340380)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/quad/jsonld/jsonld.go:34 +0x13b
github.com/cayleygraph/cayley/quad/jsonld.init.1.func2(0x2340380, 0xc420231380, 0xc420231380, 0x2340380)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/quad/jsonld/jsonld.go:24 +0x35
github.com/cayleygraph/cayley/internal/http.(*API).ServeV2Write(0xc4200666d0, 0x1ef1fa0, 0xc4200a0c40, 0xc420221d00, 0x0, 0x0, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/internal/http/write.go:215 +0x285
github.com/cayleygraph/cayley/internal/http.(*API).ServeV2Write-fm(0x1ef1fa0, 0xc4200a0c40, 0xc420221d00, 0x0, 0x0, 0x0, 0x1a00655)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/internal/http/http.go:172 +0x66
github.com/cayleygraph/cayley/internal/http.LogRequest.func1(0x1ef1fa0, 0xc4200a0c40, 0xc420221d00, 0x0, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/internal/http/http.go:84 +0x221
github.com/cayleygraph/cayley/internal/http.CORS.func1(0x1ef1fa0, 0xc4200a0c40, 0xc420221d00, 0x0, 0x0, 0x0)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/internal/http/http.go:159 +0xaf
github.com/cayleygraph/cayley/vendor/github.com/julienschmidt/httprouter.(*Router).ServeHTTP(0xc4202151d0, 0x1ef1fa0, 0xc4200a0c40, 0xc420221d00)
/home/barak/src/cayley/src/github.com/cayleygraph/cayley/vendor/github.com/julienschmidt/httprouter/router.go:299 +0x750
net/http.(*ServeMux).ServeHTTP(0x2076200, 0x1ef1fa0, 0xc4200a0c40, 0xc420221d00)
/usr/lib/go/src/net/http/server.go:2238 +0x130
net/http.serverHandler.ServeHTTP(0xc4202a8160, 0x1ef1fa0, 0xc4200a0c40, 0xc420221d00)
/usr/lib/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc42021f180, 0x1ef2ca0, 0xc420231280)
/usr/lib/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
/usr/lib/go/src/net/http/server.go:2668 +0x2ce
can you help me ? thank you.

This was a bug. Should be fixed in a new version of Cayley.

Related

Get length of array of unsigned chars in C

How can I get length of array which is array of unsigned char*?
This is my array:
unsigned char ot[] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0, 0x0, 0x0, 0x0, 0x0 };
I tried to use strlen() like this:
int length = strlen((char*) ot);
It returns me length of 11 till first 0x0, but what is my array changes to this? (check last element)
unsigned char ot[] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0, 0x0, 0x0, 0x0, 0xa1 };
Then I get still 11 elements, how can I get "real" length of whole array? Like what if there will be data after some 0x0 element.
For example for:
unsigned char* file_buffer = new unsigned char[BUFFER_SIZE];
fread(file_buffer,sizeof(unsigned char),BUFFER_SIZE,input_file)
What could be best solution? Because return value of fread() can vary, if I am reading last chunk of file, and what if in file will be 0x0 before some data?
How can I get length of array which is array of unsigned char* ?
According to the examples you gave you are attempting to retrieve the length of an array [] and not an array *. This can be achieved using the keyword sizeof (sizeof(ot)).
However when it comes to pointers (such as your unsigned char *), you will need to know the size beforehand as using sizeof over it would return the allocated size of the pointer itself and not the actual content size of this pointer.
unsigned char ot[] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0, 0x0, 0x0, 0x0, 0x0};
int _length = strlen((char *) ot);
printf("%d\n", _length); // 11
int length = sizeof(ot) / sizeof(unsigned char);
printf("%d\n", length); // 16
// reason behind strlen behaves like this
if (0x0 == '\0') {
printf("Yes\n");
}
strlen() returns string length when it finds null terminator which is '\0'. If you run the code, it will print Yes at the end, means 0x0 is actually equivalent to '\0' null terminator.
Use sizeof() to get the real length.

How do I create a byte array from a mix of hex and strings in Go?

I'm trying to implement the data specified here:
https://developer.valvesoftware.com/wiki/Server_queries#Request_Format
I'm creating a byte array that needs to end up looking like this:
0xFF 0xFF 0xFF 0xFF 0x54 0x53 0x6F 0x75 0x72 0x63 0x65 0x20 0x45 0x6E 0x67 0x69 0x6E 0x65 0x20 0x51 0x75 0x65 0x72 0x79 0x00
Broken down, it is just some bytes in a header:
0xFF 0xFF 0xFF 0xFF 0x54
Then after that, the zero terminated string "Source Engine Query".
I was able to get this to work in a very ugly fashion, but I know there has to be a cleaner path:
message := []byte("xxxxxSource Engine Queryx")
message[0] = 0xFF
message[1] = 0xFF
message[2] = 0xFF
message[3] = 0xFF
message[4] = 0x54
message[24] = 0x00
I've tried using slices like this, but I can't figure out how to use it with non string values:
message := make([]byte, 25)
copy(message[5:], "Source Engine Query")
That works, but then I can't figure out how to add the "0xFF 0xFF 0xFF 0xFF 0x54" to the beginning.
It's also bytes.Buffer https://golang.org/pkg/bytes/#Buffer among other approaches, quite fast and has couple of handy methods
message := bytes.NewBuffer([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0x54})
message.WriteString("Source Engine Query")
message.WriteByte(0x00)
message.WriteTo(os.Stdout) //or write to some other io.Writer you want, say net.Conn
After a long time of working on this, I of course figure it out after I ask here...
message := []byte("\xff\xff\xff\xff\x54Source Engine Query\x00")
This works perfect. I found it here:
https://blog.golang.org/strings

How to debug C-shared library (written in Go) - it hangs on net call

Go code with http.Post(). If I build exe, it's OK. If I build C shared library -buildmode=c-shared, it hangs on https.Post().
I've tried strace -fp PID:
futex(0x7f618b2c1cd0, FUTEX_WAKE, 1) = 0
futex(0xc820022110, FUTEX_WAIT, 0, NULL
I've tried to add profiler:
http.ListenAndServe("localhost:6060", nil)
http.Post()
But again it hangs on http.Post and profiler was blocked as well (it was listening, but not response from 6060 port). I've sent SIGQUIT signal - stack trace:
SIGQUIT: quit
PC=0x7f0cad5c9081 m=1
goroutine 0 [idle]:
runtime.futex(0xc820022110, 0x0, 0x0, 0x0, 0x7f0c00000000, 0x7f0cad577879, 0x0, 0x0, 0x7f0cad577ae8, 0xc820022110, ...)
/usr/local/go/src/runtime/sys_linux_amd64.s:288 +0x21
runtime.futexsleep(0xc820022110, 0xc800000000, 0xffffffffffffffff)
/usr/local/go/src/runtime/os1_linux.go:39 +0x53
runtime.notesleep(0xc820022110)
/usr/local/go/src/runtime/lock_futex.go:142 +0xa8
runtime.stoplockedm()
/usr/local/go/src/runtime/proc1.go:1268 +0xb2
runtime.schedule()
/usr/local/go/src/runtime/proc1.go:1590 +0x72
runtime.park_m(0xc820000600)
/usr/local/go/src/runtime/proc1.go:1698 +0x191
runtime.mcall(0x7f0cad5c722a)
/usr/local/go/src/runtime/asm_amd64.s:204 +0x53
goroutine 17 [IO wait, locked to thread]:
runtime.gopark(0x7f0cada4a878, 0x7f0ca594b198, 0x7f0cad993b20, 0x7, 0x1b, 0x5)
/usr/local/go/src/runtime/proc.go:185 +0x169 fp=0xc820041868 sp=0xc820041840
runtime.netpollblock(0x7f0ca594b170, 0x72, 0x7f0cac21e050)
/usr/local/go/src/runtime/netpoll.go:338 +0x160 fp=0xc8200418b0 sp=0xc820041868
net.runtime_pollWait(0x7f0ca594b170, 0x72, 0xc82000a1c0)
/usr/local/go/src/runtime/netpoll.go:157 +0x62 fp=0xc8200418d0 sp=0xc8200418b0
net.(*pollDesc).Wait(0xc82011c140, 0x72, 0x0, 0x0)
/usr/local/go/src/net/fd_poll_runtime.go:73 +0x3c fp=0xc8200418f0 sp=0xc8200418d0
net.(*pollDesc).WaitRead(0xc82011c140, 0x0, 0x0)
/usr/local/go/src/net/fd_poll_runtime.go:78 +0x38 fp=0xc820041918 sp=0xc8200418f0
net.(*netFD).accept(0xc82011c0e0, 0x0, 0x7f0ca594b268, 0xc8200b4440)
/usr/local/go/src/net/fd_unix.go:408 +0x27e fp=0xc820041a18 sp=0xc820041918
net.(*TCPListener).AcceptTCP(0xc8200b8050, 0xc820041ab0, 0x0, 0x0)
/usr/local/go/src/net/tcpsock_posix.go:254 +0x4f fp=0xc820041a78 sp=0xc820041a18
net/http.tcpKeepAliveListener.Accept(0xc8200b8050, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/net/http/server.go:2135 +0x43 fp=0xc820041ab0 sp=0xc820041a78
net/http.(*Server).Serve(0xc8200b20c0, 0x7f0ca594b230, 0xc8200b8050, 0x0, 0x0)
/usr/local/go/src/net/http/server.go:1887 +0xb5 fp=0xc820041b98 sp=0xc820041ab0
net/http.(*Server).ListenAndServe(0xc8200b20c0, 0x0, 0x0)
/usr/local/go/src/net/http/server.go:1877 +0x138 fp=0xc820041c28 sp=0xc820041b98
net/http.ListenAndServe(0x7f0cad9a6ae0, 0xe, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/net/http/server.go:1967 +0x91 fp=0xc820041c50 sp=0xc820041c28
main.Code(0xc8201122a0, 0x0, 0x0, 0x0, 0x0)
/opt/gopath/src/github.com/jangaraj/zabbix-module/zabbix_module.go:152 +0x94 fp=0xc820041d58 sp=0xc820041c50
github.com/cavaliercoder/g2z.route_item(0x7ffe7bc803e0, 0x7ffe7bc80c90, 0x0)
/opt/gopath/src/github.com/cavaliercoder/g2z/router.go:65 +0xc2a fp=0xc820041ed0 sp=0xc820041d58
runtime.call32(0x0, 0x7ffe7bc802d8, 0x7ffe7bc80360, 0x18)
/usr/local/go/src/runtime/asm_amd64.s:437 +0x40 fp=0xc820041ef8 sp=0xc820041ed0
runtime.cgocallbackg1()
/usr/local/go/src/runtime/cgocall.go:252 +0x110 fp=0xc820041f30 sp=0xc820041ef8
runtime.cgocallbackg()
/usr/local/go/src/runtime/cgocall.go:177 +0xd9 fp=0xc820041f90 sp=0xc820041f30
runtime.cgocallback_gofunc(0x0, 0x0, 0x0)
/usr/local/go/src/runtime/asm_amd64.s:801 +0x5d fp=0xc820041fa0 sp=0xc820041f90
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1696 +0x1 fp=0xc820041fa8 sp=0xc820041fa0
goroutine 18 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1696 +0x1
goroutine 5 [chan receive]:
github.com/golang/glog.(*loggingT).flushDaemon(0x7f0cadd99bc0)
/opt/gopath/src/github.com/golang/glog/glog.go:882 +0x69
created by github.com/golang/glog.init.1
/opt/gopath/src/github.com/golang/glog/glog.go:410 +0x299
rax 0xca
rbx 0x0
rcx 0xffffffffffffffff
rdx 0x0
rdi 0xc820022110
rsi 0x0
rbp 0x1
rsp 0x7ffe7bc801a8
r8 0x0
r9 0x0
r10 0x0
r11 0x286
r12 0x4
r13 0x7f0cada47090
r14 0xd
r15 0x8
rip 0x7f0cad5c9081
rflags 0x286
cs 0x33
fs 0x0
gs 0x0
How to debug/solve this problem? Code looks OK - standard executable binary returns correct value. Only C shared library version has some issue. Another methods from shared library, which don't use http.post()/net.Dial() are OK.
Original issue: https://github.com/cavaliercoder/g2z/issues/5
Ok I solved this for my use case, you might be in a similar situation.
When you link your Go c-shared library to your C or C++ application, the Go runtime is loaded when you start the application (you can verify this, in func init() for example, it's called directly when you start your app). Then, if you fork a process and use the Go library, you will have unpredictable behaviour.
Basically, you must load the Go shared library after fork and can't rely on it once you forked. Using dlopen and dlsym will allow you to control when the Go runtime get loaded.

Arduino PROGMEM byte array

Consider this snippet, compiled with Arduino IDE:
PROGMEM char charSet[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xA , 0x6, 0xE, 0x1, 0x9,0x5, 0xD, 0x3,0xB,0x7,0xF };
char reversed[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xA , 0x6, 0xE, 0x1, 0x9,0x5, 0xD, 0x3,0xB,0x7,0xF };
char ff;
for (int i=0; i < 16; i++) {
Serial.print(" ");
Serial.print(reversed[i], HEX);
}
Serial.println(" ");
for (int i=0; i < 16; i++) {
Serial.print(" ");
ff = pgm_read_byte(&charSet[i]);
Serial.print(ff);
}
I would expect the two for loops to produce same output. But the output is:
0 8 4 C 2 A 6 E 1 9 5 D 3 B 7 F
FFFFFF94 FFFFFFB0 6 FFFFFF80 FFFFFF91 FFFFFFC7 3 62 FFFFFFE3 E FFFFFF94 5E 29 FFFFFF99 23 39
What am I missing?
Yes, the const keyword is missing.
Probably it is not a good idea to use prog_char, as it is marked as deprecated in gcc-avr, see avr/pgmspace.h
Well the answer was the PROGMEM declaration. I had to move it out from function, adding const keyword. Other changes as datatypes were not working.
The full answer is on Arduino forum.
You have the wrong datatype. From http://arduino.cc/en/Reference/PROGMEM:
Some cryptic bugs are generated by using ordinary datatypes for program memory calls.
You can't use char — you have to use prog_char
So:
PROGMEM prog_char charSet[] = {0x0, 0xB}
That should work.
by the way: You don't have to do Serial.println(" "); You can just do Serial.println();
PROGMEM char charSet[] = ...
This is not good - for PROGMEM data, you can't use ordinary in-memory types. What you should do is
PROGMEM prog_char charSet[] = ...

Initializing malloc'd memory in a way similar to an array literal?

Let's say I've calloc'd some memory for myself:
byte *header = calloc(5 + ZHEADERSIZE, sizeof(byte));
This gives me an array of 0's. But what if I want to make a custom initialization of the memory? The following code is a fairly detailed initialization for an array literal. Note that it actually contains variables, so I can't just memcpy all of the array over. I'm wondering if I can replicate this style of initialization for a block of memory that is malloc'd out without having to insert them manually.
unsigned char zhead[] =
{
0x00, 37, 0, 218, 0xFF,
0x50, 0x4b, 0x03, 0x04,
0x14,
0x00,
0x00, 0x00,
0x08, 0x00,
0x08, 0x03,
0x64, 0x3c,
0xAA, 0xBB, 0xCC, 0xDD,
csize, (csize>>8), 0, 0,
uncsize, (uncsize>>8), 0, 0,
0x07, 0x00,
0x00, 0x00,
'r', '/', 'r', '.', 'z', 'i', 'p'
};
You cannot initialize dynamically allocated memory in C other than to zero using calloc. You can however provide a static prototype array from which you copy the data:
const char prototype[] = { 'a', 'b', 'c' };
int main()
{
char * data = malloc(sizeof prototype);
memcpy(data, prototype, sizeof prototype);
data[1] = 'z'; // fill in custom data
}

Resources