Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fake_http.cc
Go to the documentation of this file.
1// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include <atomic>
6#include <chrono>
7#include <cstring>
8#include <thread>
9
10#if defined(_WIN32)
11#define DART_EXPORT extern "C" __declspec(dllexport)
12#else
13#define DART_EXPORT \
14 extern "C" __attribute__((visibility("default"))) __attribute((used))
15#endif
16
17constexpr char kExampleRequest[] = R"(
18GET / HTTP/1.1
19Host: www.example.com
20)";
21
22constexpr char kExampleResponse[] = R"(
23HTTP/1.1 200 OK
24Content-Length: 54
25Content-Type: text/html; charset=UTF-8
26
27<html>
28 <body>
29 Hello world!
30 </body>
31</html>
32)";
33
34DART_EXPORT void http_get(const char* uri, void (*onResponse)(const char*)) {
35 std::thread([onResponse]() {
36 std::this_thread::sleep_for(std::chrono::seconds(3));
37 onResponse(strdup(kExampleResponse));
38 }).detach();
39}
40
41DART_EXPORT void http_serve(void (*onRequest)(const char*)) {
42 std::thread([onRequest]() {
43 while (true) {
44 std::this_thread::sleep_for(std::chrono::seconds(1));
45 onRequest(strdup(kExampleRequest));
46 }
47 }).detach();
48}
DART_EXPORT void http_serve(void(*onRequest)(const char *))
Definition fake_http.cc:41
DART_EXPORT void http_get(const char *uri, void(*onResponse)(const char *))
Definition fake_http.cc:34
#define DART_EXPORT
Definition fake_http.cc:13
constexpr char kExampleRequest[]
Definition fake_http.cc:17
constexpr char kExampleResponse[]
Definition fake_http.cc:22