Changeset a1551f182d761cecc6451e8f8cc5b5f1b7d0d559 for src/modules
- Timestamp:
- 09/18/09 13:30:43 (4 years ago)
- git-parent:
- Files:
-
- src/modules/lua_noit.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/modules/lua_noit.c
rd661166 ra1551f1 213 213 luaL_error(L, "must be called as method"); 214 214 e = *eptr; 215 c a= lua_tostring(L, 2);216 ciphers= lua_tostring(L, 3);217 c ert= lua_tostring(L, 4);218 key= lua_tostring(L, 5);215 cert = lua_tostring(L, 2); 216 key = lua_tostring(L, 3); 217 ca = lua_tostring(L, 4); 218 ciphers = lua_tostring(L, 5); 219 219 220 220 sslctx = eventer_ssl_ctx_new(SSL_CLIENT, cert, key, ca, ciphers); … … 239 239 240 240 static int 241 noit_lua_socket_ read_complete(eventer_t e, int mask, void *vcl,242 struct timeval *now) {241 noit_lua_socket_do_read(eventer_t e, int *mask, struct nl_slcl *cl, 242 int *read_complete) { 243 243 char buff[4096]; 244 noit_lua_check_info_t *ci;245 struct nl_slcl *cl = vcl;246 244 int len; 247 int args = 0; 248 249 ci = get_ci(cl->L); 250 assert(ci); 251 252 if(mask & EVENTER_EXCEPTION) { 253 lua_pushnil(cl->L); 254 args = 1; 255 goto alldone; 256 } 257 258 while((len = e->opset->read(e->fd, buff, sizeof(buff), &mask, e)) > 0) { 245 *read_complete = 0; 246 while((len = e->opset->read(e->fd, buff, sizeof(buff), mask, e)) > 0) { 259 247 if(cl->read_goal) { 260 248 int remaining = cl->read_goal - cl->read_sofar; … … 264 252 if(cl->read_sofar >= cl->read_goal) { /* We're done */ 265 253 lua_pushlstring(cl->L, cl->inbuff, cl->read_goal); 266 args= 1;254 *read_complete = 1; 267 255 cl->read_sofar -= cl->read_goal; 268 256 if(cl->read_sofar > 0) { /* We have to buffer this for next read */ … … 283 271 if(cp) { 284 272 lua_pushlstring(cl->L, cl->inbuff, cl->inbuff_len); 285 args= 1;273 *read_complete = 1; 286 274 287 275 cl->read_sofar = len - remaining; … … 294 282 } 295 283 } 284 return len; 285 } 286 static int 287 noit_lua_socket_read_complete(eventer_t e, int mask, void *vcl, 288 struct timeval *now) { 289 noit_lua_check_info_t *ci; 290 struct nl_slcl *cl = vcl; 291 int len; 292 int args = 0; 293 294 ci = get_ci(cl->L); 295 assert(ci); 296 297 if(mask & EVENTER_EXCEPTION) { 298 lua_pushnil(cl->L); 299 args = 1; 300 goto alldone; 301 } 302 303 len = noit_lua_socket_do_read(e, &mask, cl, &args); 296 304 if(len >= 0) { 297 305 /* We broke out, cause we read enough... */ … … 316 324 static int 317 325 noit_lua_socket_read(lua_State *L) { 326 int args, mask, len; 318 327 struct nl_slcl *cl; 319 328 noit_lua_check_info_t *ci; … … 367 376 } 368 377 378 len = noit_lua_socket_do_read(e, &mask, cl, &args); 379 if(args == 1) return 1; /* completed read, return result */ 380 if(len == -1 && errno == EAGAIN) { 381 /* we need to drop into eventer */ 382 } 383 else { 384 lua_pushnil(cl->L); 385 args = 1; 386 return args; 387 } 388 369 389 e->callback = noit_lua_socket_read_complete; 370 e->mask = EVENTER_READ| EVENTER_EXCEPTION;390 e->mask = mask | EVENTER_EXCEPTION; 371 391 eventer_add(e); 372 392 return noit_lua_yield(ci, 0); … … 458 478 } 459 479 lua_pushinteger(L, -1); 480 return 1; 481 } 482 static int 483 noit_lua_socket_ssl_ctx(lua_State *L) { 484 eventer_t *eptr, e; 485 eventer_ssl_ctx_t **ssl_ctx_holder, *ssl_ctx; 486 487 eptr = lua_touserdata(L, lua_upvalueindex(1)); 488 if(eptr != lua_touserdata(L, 1)) 489 luaL_error(L, "must be called as method"); 490 e = *eptr; 491 492 ssl_ctx = eventer_get_eventer_ssl_ctx(e); 493 if(!ssl_ctx) { 494 lua_pushnil(L); 495 return 1; 496 } 497 498 ssl_ctx_holder = (eventer_ssl_ctx_t **)lua_newuserdata(L, sizeof(ssl_ctx)); 499 *ssl_ctx_holder = ssl_ctx; 500 luaL_getmetatable(L, "noit.eventer.ssl_ctx"); 501 lua_setmetatable(L, -2); 460 502 return 1; 461 503 } … … 497 539 return 1; 498 540 } 541 if(!strcmp(k, "ssl_ctx")) { 542 lua_pushlightuserdata(L, udata); 543 lua_pushcclosure(L, noit_lua_socket_ssl_ctx, 1); 544 return 1; 545 } 499 546 break; 500 547 case 'w': … … 508 555 break; 509 556 } 510 luaL_error(L, " eventer_tno such element: %s", k);557 luaL_error(L, "noit.eventer no such element: %s", k); 511 558 return 0; 512 559 } … … 520 567 lua_setmetatable(L, -2); 521 568 return addr; 569 } 570 571 static int 572 noit_ssl_ctx_index_func(lua_State *L) { 573 int n; 574 const char *k; 575 eventer_ssl_ctx_t **udata, *ssl_ctx; 576 n = lua_gettop(L); /* number of arguments */ 577 assert(n == 2); 578 if(!luaL_checkudata(L, 1, "noit.eventer.ssl_ctx")) { 579 luaL_error(L, "metatable error, arg1 not a noit.eventer.ssl_ctx!"); 580 } 581 udata = lua_touserdata(L, 1); 582 ssl_ctx = *udata; 583 if(!lua_isstring(L, 2)) { 584 luaL_error(L, "metatable error, arg2 not a string!"); 585 } 586 k = lua_tostring(L, 2); 587 switch(*k) { 588 case 'e': 589 if(!strcmp(k,"error")) { 590 lua_pushstring(L,eventer_ssl_get_peer_error(ssl_ctx)); 591 return 1; 592 } 593 if(!strcmp(k,"end_time")) { 594 lua_pushinteger(L,eventer_ssl_get_peer_end_time(ssl_ctx)); 595 return 1; 596 } 597 break; 598 case 'i': 599 if(!strcmp(k,"issuer")) { 600 lua_pushstring(L,eventer_ssl_get_peer_issuer(ssl_ctx)); 601 return 1; 602 } 603 break; 604 case 's': 605 if(!strcmp(k,"subject")) { 606 lua_pushstring(L,eventer_ssl_get_peer_subject(ssl_ctx)); 607 return 1; 608 } 609 if(!strcmp(k,"start_time")) { 610 lua_pushinteger(L,eventer_ssl_get_peer_start_time(ssl_ctx)); 611 return 1; 612 } 613 break; 614 default: 615 break; 616 } 617 luaL_error(L, "noit.eventer.ssl_ctx no such element: %s", k); 618 return 0; 522 619 } 523 620 … … 809 906 } 810 907 908 static int 909 nl_conf_get_string(lua_State *L) { 910 char *val; 911 const char *path = lua_tostring(L,1); 912 if(path && 913 noit_conf_get_string(NULL, path, &val)) { 914 lua_pushstring(L,val); 915 free(val); 916 } 917 else lua_pushnil(L); 918 return 1; 919 } 920 static int 921 nl_conf_get_integer(lua_State *L) { 922 int val; 923 const char *path = lua_tostring(L,1); 924 if(path && 925 noit_conf_get_int(NULL, path, &val)) { 926 lua_pushinteger(L,val); 927 } 928 else lua_pushnil(L); 929 return 1; 930 } 931 static int 932 nl_conf_get_boolean(lua_State *L) { 933 noit_boolean val; 934 const char *path = lua_tostring(L,1); 935 if(path && 936 noit_conf_get_boolean(NULL, path, &val)) { 937 lua_pushboolean(L,val); 938 } 939 else lua_pushnil(L); 940 return 1; 941 } 942 static int 943 nl_conf_get_float(lua_State *L) { 944 float val; 945 const char *path = lua_tostring(L,1); 946 if(path && 947 noit_conf_get_float(NULL, path, &val)) { 948 lua_pushnumber(L,val); 949 } 950 else lua_pushnil(L); 951 return 1; 952 } 811 953 static const luaL_Reg noitlib[] = { 812 954 { "sleep", nl_sleep }, … … 817 959 { "socket_ipv6", nl_socket_ipv6 }, 818 960 { "gunzip", nl_gunzip }, 961 { "conf_get", nl_conf_get_string }, 962 { "conf_get_string", nl_conf_get_string }, 963 { "conf_get_integer", nl_conf_get_integer }, 964 { "conf_get_boolean", nl_conf_get_boolean }, 965 { "conf_get_number", nl_conf_get_float }, 819 966 { NULL, NULL } 820 967 }; … … 823 970 luaL_newmetatable(L, "noit.eventer"); 824 971 lua_pushcclosure(L, noit_eventer_index_func, 0); 972 lua_setfield(L, -2, "__index"); 973 974 luaL_newmetatable(L, "noit.eventer.ssl_ctx"); 975 lua_pushcclosure(L, noit_ssl_ctx_index_func, 0); 825 976 lua_setfield(L, -2, "__index"); 826 977
