Skip to content

Commit 3055e1d

Browse files
committed
Merge pull request #63 from moteus/master
Fix. call all callback from coroutine where `perform` was called.
2 parents f42a0e4 + f282bc3 commit 3055e1d

File tree

12 files changed

+200
-166
lines changed

12 files changed

+200
-166
lines changed

.travis.yml

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,45 @@ language: c
22

33
sudo: false
44

5-
env:
6-
global:
7-
- LUAROCKS=2.2.2
8-
matrix:
9-
- LUA=lua5.1
10-
- LUA=lua5.2
11-
- LUA=lua5.3
12-
- LUA=luajit
5+
matrix:
6+
include:
7+
- compiler: ": Lua51"
8+
env: LUA="lua 5.1"
9+
- compiler: ": Lua52"
10+
env: LUA="lua 5.2"
11+
- compiler: ": Lua53"
12+
env: LUA="lua 5.3"
13+
- compiler: ": LuaJIT20"
14+
env: LUA="luajit 2.0"
15+
- compiler: ": LuaJIT21"
16+
env: LUA="luajit 2.1"
17+
18+
cache:
19+
directories:
20+
- here
21+
- $HOME/.cache/pip
1322

1423
branches:
1524
only:
1625
- master
1726

1827
before_install:
19-
- source .travis/setenv_lua.sh
28+
- export CC=gcc
2029
- pip install --user cpp-coveralls
21-
- luarocks install luafilesystem --from=https://rocks.moonscript.org/dev
22-
- luarocks install luacov-coveralls
23-
- luarocks install lunitx
24-
- luarocks install dkjson --deps-mode=none
30+
- pip install --user hererocks
31+
- hererocks here -r^ --$LUA
32+
- export PATH=$PATH:$PWD/here/bin
2533

2634
install:
2735
- luarocks make rockspecs/lua-curl-scm-0.rockspec CFLAGS="-O2 -fPIC -ftest-coverage -fprofile-arcs" LIBFLAG="-shared --coverage"
2836

29-
script:
37+
before_script:
38+
- luarocks show luacov-coveralls || luarocks install luacov-coveralls
39+
- luarocks show lunitx || luarocks install lunitx
40+
- luarocks show luafilesystem || luarocks install luafilesystem
41+
- luarocks show dkjson || luarocks install dkjson --deps-mode=none
42+
43+
script:
3044
- cd test
3145
- lua -e "print(require 'cURL.utils'.find_ca_bundle())"
3246
- lunit.sh test_easy.lua
@@ -37,7 +51,7 @@ script:
3751

3852
after_success:
3953
- coveralls -b .. -r .. --dump c.report.json
40-
- luacov-coveralls -j c.report.json
54+
- luacov-coveralls -j c.report.json -v
4155

4256
notifications:
4357
email:

.travis/platform.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

.travis/setenv_lua.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

.travis/setup_lua.sh

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/lceasy.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ int lcurl_easy_create(lua_State *L, int error_mode){
4141

4242
p->err_mode = error_mode;
4343
if(!p->curl) return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, CURLE_FAILED_INIT);
44-
p->L = L;
44+
45+
p->magic = LCURL_EASY_MAGIC;
46+
p->L = NULL;
47+
p->post = NULL;
4548
p->storage = lcurl_storage_init(L);
4649
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
4750
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
@@ -115,6 +118,12 @@ static int lcurl_easy_perform(lua_State *L){
115118

116119
assert(p->rbuffer.ref == LUA_NOREF);
117120

121+
// store reference to current coroutine to callbacks
122+
p->L = L;
123+
if(p->post){
124+
p->post->L = L;
125+
}
126+
118127
code = curl_easy_perform(p->curl);
119128

120129
if(p->rbuffer.ref != LUA_NOREF){
@@ -308,6 +317,8 @@ static int lcurl_easy_set_HTTPPOST(lua_State *L){
308317
curl_easy_setopt(p->curl, CURLOPT_READFUNCTION, lcurl_hpost_read_callback);
309318
}
310319

320+
p->post = post;
321+
311322
lua_settop(L, 1);
312323
return 1;
313324
}
@@ -420,6 +431,8 @@ static int lcurl_easy_unset_HTTPPOST(lua_State *L){
420431
lcurl_storage_remove_i(L, p->storage, CURLOPT_HTTPPOST);
421432
}
422433

434+
p->post = NULL;
435+
423436
lua_settop(L, 1);
424437
return 1;
425438
}
@@ -758,12 +771,15 @@ static size_t lcurl_read_callback(lua_State *L,
758771

759772
static size_t lcurl_easy_read_callback(char *buffer, size_t size, size_t nitems, void *arg){
760773
lcurl_easy_t *p = arg;
774+
if(p->magic == LCURL_HPOST_STREAM_MAGIC){
775+
return lcurl_hpost_read_callback(buffer, size, nitems, arg);
776+
}
761777
return lcurl_read_callback(p->L, &p->rd, &p->rbuffer, buffer, size, nitems);
762778
}
763779

764780
static size_t lcurl_hpost_read_callback(char *buffer, size_t size, size_t nitems, void *arg){
765781
lcurl_hpost_stream_t *p = arg;
766-
return lcurl_read_callback(p->L, &p->rd, &p->rbuffer, buffer, size, nitems);
782+
return lcurl_read_callback(*p->L, &p->rd, &p->rbuffer, buffer, size, nitems);
767783
}
768784

769785
static int lcurl_easy_set_READFUNCTION(lua_State *L){
@@ -1035,9 +1051,8 @@ void lcurl_easy_initlib(lua_State *L, int nup){
10351051
/* Hack. We ensure that lcurl_easy_t and lcurl_hpost_stream_t
10361052
compatiable for readfunction
10371053
*/
1038-
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, L) == offsetof(lcurl_hpost_stream_t, L));
1039-
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, rd) == offsetof(lcurl_hpost_stream_t, rd));
1040-
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, rbuffer) == offsetof(lcurl_hpost_stream_t, rbuffer));
1054+
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, magic) == offsetof(lcurl_hpost_stream_t, magic));
1055+
LCURL_STATIC_ASSERT(sizeof(((lcurl_easy_t*)0)->magic) == sizeof(((lcurl_hpost_stream_t*)0)->magic));
10411056

10421057
if(!lutil_createmetap(L, LCURL_EASY, lcurl_easy_methods, nup))
10431058
lua_pop(L, nup);

src/lceasy.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "lcurl.h"
1515
#include "lcutils.h"
16+
#include "lchttppost.h"
1617

1718
#define LCURL_LST_INDEX(N) LCURL_##N##_LIST,
1819
#define LCURL_STR_INDEX(N)
@@ -32,11 +33,17 @@ enum {
3233
#undef LCURL_LNG_INDEX
3334
#undef OPT_ENTRY
3435

36+
#define LCURL_EASY_MAGIC 0xEA
37+
3538
typedef struct lcurl_easy_tag{
39+
unsigned char magic;
40+
3641
lua_State *L;
3742
lcurl_callback_t rd;
3843
lcurl_read_buffer_t rbuffer;
3944

45+
lcurl_hpost_t *post;
46+
4047
CURL *curl;
4148
int storage;
4249
int lists[LCURL_LIST_COUNT];

src/lchttppost.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ static lcurl_hpost_stream_t *lcurl_hpost_stream_add(lua_State *L, lcurl_hpost_t
2323
lcurl_hpost_stream_t *stream = malloc(sizeof(lcurl_hpost_stream_t));
2424
if(!stream) return NULL;
2525

26-
stream->L = L;
26+
stream->magic = LCURL_HPOST_STREAM_MAGIC;
27+
stream->L = &p->L;
2728
stream->rbuffer.ref = LUA_NOREF;
2829
stream->rd.cb_ref = stream->rd.ud_ref = LUA_NOREF;
2930
stream->next = NULL;

src/lchttppost.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
#include "lcutils.h"
1616
#include <stdlib.h>
1717

18+
#define LCURL_HPOST_STREAM_MAGIC 0xAA
19+
1820
typedef struct lcurl_hpost_stream_tag{
19-
lua_State *L;
21+
unsigned char magic;
22+
23+
lua_State **L;
2024
lcurl_callback_t rd;
2125
lcurl_read_buffer_t rbuffer;
2226
struct lcurl_hpost_stream_tag *next;
2327
}lcurl_hpost_stream_t;
2428

2529
typedef struct lcurl_hpost_tag{
30+
lua_State *L;
2631
struct curl_httppost *post;
2732
struct curl_httppost *last;
2833
int storage;

src/lcmulti.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ static int lcurl_multi_perform(lua_State *L){
118118
lcurl_multi_t *p = lcurl_getmulti(L);
119119
int running_handles = 0;
120120
CURLMcode code;
121+
122+
lua_settop(L, 1);
123+
lua_rawgeti(L, LCURL_LUA_REGISTRY, p->h_ref);
124+
lua_pushnil(L);
125+
while(lua_next(L, 2)){
126+
lcurl_easy_t *e = lcurl_geteasy_at(L, -1);
127+
e->L = L;
128+
if(e->post){
129+
e->post->L = L;
130+
}
131+
lua_pop(L, 1);
132+
}
133+
134+
lua_settop(L, 1);
135+
121136
while((code = curl_multi_perform(p->curl, &running_handles)) == CURLM_CALL_MULTI_PERFORM);
122137
if(code != CURLM_OK){
123138
lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_MULTI, code);

test/.luacov

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ return {
3232
-- configuration for luacov-coveralls reporter
3333
coveralls = {
3434
pathcorrect = {
35-
{"/usr/local/share/lua/5.[123]", "src/lua"};
35+
{"^.-[/\\]share[/\\]lua[/\\]5.%d", "src/lua"};
3636
},
3737
},
3838
}

0 commit comments

Comments
 (0)