Skip to content

Commit

Permalink
ch07 progress
Browse files Browse the repository at this point in the history
  • Loading branch information
spamegg1 committed Aug 6, 2024
1 parent 2d57207 commit 825b7f1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scripts/installGatling.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env -S scala-cli
//> using dep com.lihaoyi::os-lib:0.10.2
//> using dep com.lihaoyi::os-lib:0.10.3

// download Gatling Highcharts Bundle
println("Downloading Gatling 3.10.5 to directory ./gatling")
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/ch07/common/common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch07

import collection.mutable.{Map => MMap}

// used in curlSync, curlAsync
case class ResponseState(
var code: Int = 200,
var headers: MMap[String, String] = MMap(),
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/ch07/common/libcurl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object LibCurl:
type MultiCurl = Ptr[Byte]
type CurlRequest = CStruct4[Ptr[Byte], Long, Long, Int]
type CurlMessage = CStruct3[Int, Curl, Ptr[Byte]]
type CurlSList = CStruct2[Ptr[Byte], CString]
type CurlSList = CStruct2[Ptr[Byte], CString] // linked list for Http headers
type CurlDataCallback = CFuncPtr4[Ptr[Byte], CSize, CSize, Ptr[Byte], CSize]
type CurlSocketCallback = CFuncPtr5[Curl, Ptr[Byte], CInt, Ptr[Byte], Ptr[Byte], CInt]
type CurlTimerCallback = CFuncPtr3[MultiCurl, Long, Ptr[Byte], CInt]
Expand All @@ -35,12 +35,12 @@ object LibCurl:
def curl_easy_setopt(handle: Curl, option: CurlOption, parameter: CVarArgList): CInt =
extern

@name("curl_easy_setopt")
@name("curl_easy_setopt") // convert URL to CString, pass it to easy_setopt
def curl_easy_setopt(handle: Curl, option: CurlOption, parameter: Ptr[Byte]): CInt =
extern

@name("curl_easy_getinfo")
def easy_getinfo(handle: Curl, info: CInt, parameter: Ptr[Byte]): CInt = extern
def easy_getinfo(handle: Curl, info: CurlInfo, parameter: Ptr[Byte]): CInt = extern

@name("curl_multi_add_handle")
def multi_add_handle(multi: MultiCurl, easy: Curl): Int = extern
Expand Down
14 changes: 13 additions & 1 deletion src/main/scala/ch07/curlSync/main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,24 @@ object CurlBasic:

size * nmemb

// .+? : match any chars one or more times, but shortest match possible until space.
// (\d+) : match any digits one or more times. (capture group)
// (.+) : match any chars one or more times, as long as possible, until space. (group)
// For example: .+? (\d+) (.+)
// HTTP/1.1 200 OK
val statusLine = raw".+? (\d+) (.+)\n".r

// ([^:]+) : match any chars except : one or more times. (capture group)
// (.*) : match any chars 0 or more times, as long as possible, until space. (group)
// For example, ([^:]+): ( .* )
// Host: 192.168.1.1
// Port: 8080
val headerLine = raw"([^:]+): (.*)\n".r

val headerCB = CFuncPtr4.fromScalaFunction[Ptr[Byte], CSize, CSize, Ptr[Byte], CSize]:
(ptr: Ptr[Byte], size: CSize, nmemb: CSize, data: Ptr[Byte]) =>
val serial = !(data.asInstanceOf[Ptr[Long]])
val len = stackalloc[Double]()
val len = stackalloc[Double](1)
!len = 0
val byteSize = size * nmemb
val headerString = bufferToString(ptr, size, nmemb)
Expand Down

0 comments on commit 825b7f1

Please sign in to comment.