-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshioridll.nim
174 lines (143 loc) · 6.21 KB
/
shioridll.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
##[
The SHIORI DLL interface
This is released under `MIT License <https://narazaka.net/license/MIT?2017>`_.
- `Repository <https://github.com/Narazaka/shioridll-nim>`_
How to make SHIORI.DLL
=============
write your myshiori.nim like below...
.. code-block:: Nim
import shioridll
import strutils
shioriLoadCallback = proc(str: string): bool =
true
shioriRequestCallback = proc(str: string): string =
if str.contains("SHIORI/2"):
"SHIORI/3.0 400 Bad Reuqest\nCharset: UTF-8\nSender: nimshiori\n\n"
elif str.contains("ID: name"):
"SHIORI/3.0 200 OK\nCharset: UTF-8\nSender: nimshiori\nValue: nimshiori\n\n"
elif str.contains("ID: version"):
"SHIORI/3.0 200 OK\nCharset: UTF-8\nSender: nimshiori\nValue: 0.0.1\n\n"
elif str.contains("ID: craftman"):
"SHIORI/3.0 200 OK\nCharset: UTF-8\nSender: nimshiori\nValue: narazaka\n\n"
elif str.contains("ID: OnBoot"):
"SHIORI/3.0 200 OK\nCharset: UTF-8\nSender: nimshiori\nValue: \\0\\s[0]aaaaaa\\e\n\n"
else:
"SHIORI/3.0 204 No Content\nCharset: UTF-8\nSender: nimshiori\n\n"
shioriUnloadCallback = proc(): bool =
true
# for test
when appType != "lib":
main("C:\\ssp\\ghost\\nim\\", @[
"GET SHIORI/3.0\nCharset: UTF-8\nSender: embryo\nID: version\n\n",
"GET SHIORI/3.0\nCharset: UTF-8\nSender: embryo\nID: OnBoot\n\n",
])
then make 32bit dll...
.. code-block::
nim c --app:lib -d:release --cc:vcc --cpu:i386 myshiori.nim
You also may be able to use gcc or some other compilers.
Useful way
=============
You can use `shiori <https://github.com/Narazaka/shiori-nim>`_ module for parsing SHIORI request and building SHIORI response
and can use `shiori_charset_convert <https://github.com/Narazaka/shiori_charset_convert-nim>`_ module for auto converting charset
as below...
.. code-block:: Nim
import shioridll
import shiori
import shiori_charset_convert
import tables
var dirpath: string
shioriLoadCallback = proc(dirpathStr: string): bool =
dirpath = dirpathStr
true
shioriRequestCallback = autoConvertShioriMessageCharset(proc(requestStr: string): string =
let request = parseRequest(requestStr)
var response = newResponse(headers = {"Charset": "UTF-8", "Sender": "nimshiori"}.newOrderedTable)
if request.version != "3.0":
response.statusCode = 400
return $response
case request.id:
of "name":
response.value = "nimshiori"
of "version":
response.value = "0.0.1"
of "craftman":
response.value = "narazaka"
of "OnBoot":
response.value = r"\0\s[0]aaaaaa\e"
else:
response.status = Status.No_Content
$response
)
shioriUnloadCallback = proc(): bool =
true
# for test
when appType != "lib":
main("C:\\ssp\\ghost\\nim\\", @[
"GET SHIORI/3.0\nCharset: UTF-8\nSender: embryo\nID: version\n\n",
"GET SHIORI/3.0\nCharset: UTF-8\nSender: embryo\nID: OnBoot\n\n",
])
]##
when defined(windows):
import std/winlean
type MemoryHandle = Handle
type RawHandle = ptr int
proc GlobalAlloc(uFlags: cuint, dwBytes: Natural): RawHandle {.importc,header:"<windows.h>".}
proc GlobalFree(hMem: RawHandle): RawHandle {.importc,header:"<windows.h>".}
const GMEM_FIXED: cuint = 0x0
proc shioriAlloc(size: Natural): MemoryHandle {.inline.} = cast[MemoryHandle](GlobalAlloc(GMEM_FIXED, size))
proc shioriFree(p: MemoryHandle): void {.inline.} = discard GlobalFree(cast[RawHandle](p))
else:
type MemoryHandle = ptr cchar
proc shioriAlloc(size: Natural): MemoryHandle {.inline.} = cast[ptr cchar](alloc(size))
proc shioriFree(p: MemoryHandle): void {.inline.} = dealloc(p)
var shioriLoadCallback*: proc(dirpath: string): bool ## SHIORI load()
var shioriRequestCallback*: proc(requestStr: string): string ## SHIORI request()
var shioriUnloadCallback*: proc(): bool ## SHIORI unload()
proc load(h: MemoryHandle, len: clong): bool {.cdecl,exportc,dynlib.} =
var dirpathStrPtr: cstring = cast[cstring](alloc(sizeof(cchar) * (len + 1)))
copyMem(dirpathStrPtr, cast[cstring](h), len)
shioriFree(h)
dirpathStrPtr[len] = '\0'
let dirpathStr = $dirpathStrPtr
dealloc(dirpathStrPtr)
shioriLoadCallback(dirpathStr)
proc unload(): bool {.cdecl,exportc,dynlib.} =
shioriUnloadCallback()
proc request(h: MemoryHandle, len: ptr clong): MemoryHandle {.cdecl,exportc,dynlib.} =
var requestStrPtr: cstring = cast[cstring](alloc(sizeof(cchar) * (len[] + 1)))
copyMem(requestStrPtr, cast[cstring](h), len[])
shioriFree(h)
requestStrPtr[len[]] = '\0'
let requestStr = $requestStrPtr
dealloc(requestStrPtr)
let responseStr = cstring(shioriRequestCallback(requestStr))
len[] = cast[clong](responseStr.len())
var reth = shioriAlloc(sizeof(char) * (len[] + 1))
copyMem(cast[pointer](reth), responseStr, len[])
reth
# for test
when appType != "lib":
proc main*(dirpathStr: string, requestStrs: seq[string]): void =
## performs load() request()s unload() for test
##
## this will removed when --app:lib
let dirpathStrLen = dirpathStr.len()
var dirpathStrPtr: cstring = cast[cstring](shioriAlloc(sizeof(cchar) * dirpathStrLen))
copyMem(dirpathStrPtr, cstring(dirpathStr), dirpathStrLen)
echo "--\nload(" & dirpathStr & ")"
echo "result = " & $load(cast[MemoryHandle](dirpathStrPtr), cast[clong](dirpathStrLen))
for requestStr in requestStrs:
let requestStrLen = requestStr.len()
var requestStrPtr: cstring = cast[cstring](shioriAlloc(sizeof(cchar) * requestStrLen))
copyMem(requestStrPtr, cstring(requestStr), requestStrLen)
var len: ptr clong = cast[ptr clong](alloc(sizeof(clong)))
len[] = cast[clong](requestStrLen)
echo "--\nrequest(" & requestStr & ", " & $requestStrLen & ")"
var responseStrPtr = request(cast[MemoryHandle](requestStrPtr), len)
var responseStrPtr2: cstring = cast[cstring](alloc(sizeof(cchar) * (len[] + 1)))
copyMem(responseStrPtr2, cast[cstring](responseStrPtr), len[])
shioriFree(responseStrPtr)
responseStrPtr2[len[]] = '\0'
echo "result = " & $responseStrPtr2
echo "--\nunload()"
echo "result = " & $unload()