Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32 http request #70

Merged
merged 5 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/httprequest.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import windy

# HTTP requests do not block.

# All callbacks are called on the main thread.

# You can have many requests in-flight at the same time.

# Calling startHttpRequest never fails. If there is any error, onError will
# be called during the next pollEvents (or later).

let req = startHttpRequest("https://www.google.com")

req.onError = proc(msg: string) =
echo "onError: " & msg

req.onResponse = proc(response: HttpResponse) =
echo "onResponse: code=", $response.code, ", len=", response.body.len

# Closing the window exits the demo
let window = newWindow("Windy Basic", ivec2(1280, 800))
while not window.closeRequested:
pollEvents()
39 changes: 38 additions & 1 deletion src/windy/common.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pixie, unicode
import pixie, std/strutils, std/unicode

type
WindyError* = object of ValueError
Expand All @@ -18,6 +18,20 @@ type
image*: Image
hotspot*: IVec2

HttpHeader* = object
key*, value*: string

HttpRequestHandle* = distinct int
WebSocketHandle* = distinct int

HttpResponse* = ref object
code*: int
headers*: seq[HttpHeader]
body*: string

WebSocketMessageKind* = enum
Utf8Message, BinaryMessage

ClipboardContentKind* = enum
TextContent, ImageContent

Expand All @@ -30,6 +44,10 @@ type
Callback* = proc()
ButtonCallback* = proc(button: Button)
RuneCallback* = proc(rune: Rune)
HttpErrorCallback* = proc(msg: string)
HttpResponseCallback* = proc(response: HttpResponse)
HttpProgressCallback* = proc(completed, total: int)
WebSocketMessageCallback* = proc(msg: string, kind: WebSocketMessageKind)

Button* = enum
ButtonUnknown
Expand Down Expand Up @@ -149,8 +167,27 @@ type

ButtonView* = distinct set[Button]

const
defaultHttpDeadline*: float32 = -1

proc `[]`*(buttonView: ButtonView, button: Button): bool =
button in set[Button](buttonView)

proc len*(buttonView: ButtonView): int =
set[Button](buttonView).len

proc `[]`*(headers: seq[HttpHeader], key: string): string =
## Get a key out of headers. Not case sensitive.
## Use a for loop to get duplicate keys.
for header in headers:
if header.key.toLowerAscii() == key.toLowerAscii():
return header.value

proc `[]=`*(headers: var seq[HttpHeader], key, value: string) =
## Sets a key in the headers. Not case sensitive.
## Overwrites the existing header for key if it exists else adds a new header.
for header in headers.mitems:
if header.key.toLowerAscii() == key.toLowerAscii():
header.value = value
return
headers.add(HttpHeader(key: key, value: value))
13 changes: 11 additions & 2 deletions src/windy/internal.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import common, pixie
import common, pixie, std/random

const
multiClickRadius = 4
CRLF* = "\r\n"

type
State* = object
WindowState* = object
title*: string
icon*: Image
cursor*: Cursor
Expand All @@ -30,6 +31,7 @@ type
var
initialized*: bool
platformDoubleClickInterval*: float64
windyRand* = initRand(2022)

template handleButtonPressTemplate*() =
window.state.buttonDown.incl button
Expand Down Expand Up @@ -122,3 +124,10 @@ template handleRuneTemplate*() =
return
if window.onRune != nil:
window.onRune(rune)

proc addDefaultHeaders*(headers: var seq[HttpHeader]) =
if headers["user-agent"].len == 0:
headers["user-agent"] = "Windy"
if headers["accept-encoding"].len == 0:
# If there isn't a specific accept-encoding specified, enable gzip
headers["accept-encoding"] = "gzip"
2 changes: 1 addition & 1 deletion src/windy/platforms/macos/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type
onImeChange*: Callback
imePos*: IVec2

state: State
state: WindowState

inner: NSWindow
trackingArea: NSTrackingArea
Expand Down
Loading