0

I'm working on a proxy-kind thing, which should make a request via NSURLSession and progressively transfer it to the GCDWebServer response.

The issue is with GCDWebServerStreamedResponse responseWithContentType:asyncStreamBlock – it requires a content type, so must be called after I got a headers. And it returns a completion block asynchronously for some reason, while the data could be received faster.

I mean, now it works this way:

  1. API makes a call to GCDWebServer
  2. GCDWebServer creates a NSURLSession data task
  3. data task receive headers
  4. create GCDWebServerStreamedResponse
  5. receive data chunk (which I want to push into the streamed response)
  6. got the completion block from step 4

How can I get the completion block right after step 4?

Sorry if I've missed any info, just I'm stuck with it and almost sure I'm doing something wrong way...

norlin
  • 1,205
  • 11
  • 24

1 Answers1

2

You can use a NSMutableData as an intermediary buffer: whenever data arrives from the NSURLSession, append to it, and whenever GCDWebServerAsyncStreamBlock is called, return that buffer and create a new empty one.

Just be sure to lock the access to that buffer with a NSLock or a GCD serial queue.

Pol
  • 3,848
  • 1
  • 38
  • 55
  • How can I do that with no content-type? – norlin Oct 04 '16 at 19:08
  • Yep, but it looks like a dirty hack, at least I'm asking here exactly to avoid that way… – norlin Oct 05 '16 at 18:22
  • It's not a hack at all: you're trying to make 2 asynchronous systems work together, so you need a shared "state" which here is a `NSMutableData` object. – Pol Oct 10 '16 at 03:47
  • I don't see a reason why creating an async response should be asynchronous and why it has to have content-type immediately on creation. Why it's not possible to create the response an then later send headers and so on? – norlin Oct 10 '16 at 06:37
  • `GCDWebServerAsyncStreamBlock ` is a just a utility class that makes some assumptions. If you want more control, go directly to `GCDWebServerResponse` and ``. You can read more about the architecture of GCDWebServer in the README. – Pol Oct 10 '16 at 16:05
  • Thanks, I will try to dig deeper, but their readme does not provide any detailed info, just basic examples, unfortunatelly… – norlin Oct 10 '16 at 16:59