Skip to content

Allow port nil #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ install:
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
- git clone https://github.com/openresty/lua-resty-mysql.git ../lua-resty-mysql
- git clone https://github.com/openresty/lua-resty-string.git ../lua-resty-string
- git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git luajit2

Expand Down
8 changes: 5 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5181,7 +5181,7 @@ ngx.req.socket

**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua**

Returns a read-only cosocket object that wraps the downstream connection. Only [receive](#tcpsockreceive) and [receiveuntil](#tcpsockreceiveuntil) methods are supported on this object.
Returns a read-only cosocket object that wraps the downstream connection. Only [receive](#tcpsockreceive), [receiveany](#tcpsockreceiveany) and [receiveuntil](#tcpsockreceiveuntil) methods are supported on this object.

In case of error, `nil` will be returned as well as a string describing the error.

Expand All @@ -5190,7 +5190,7 @@ The socket object returned by this method is usually used to read the current re
If any request body data has been pre-read into the Nginx core request header buffer, the resulting cosocket object will take care of this to avoid potential data loss resulting from such pre-reading.
Chunked request bodies are not yet supported in this API.

Since the `v0.9.0` release, this function accepts an optional boolean `raw` argument. When this argument is `true`, this function returns a full-duplex cosocket object wrapping around the raw downstream connection socket, upon which you can call the [receive](#tcpsockreceive), [receiveuntil](#tcpsockreceiveuntil), and [send](#tcpsocksend) methods.
Since the `v0.9.0` release, this function accepts an optional boolean `raw` argument. When this argument is `true`, this function returns a full-duplex cosocket object wrapping around the raw downstream connection socket, upon which you can call the [receive](#tcpsockreceive), [receiveany](#tcpsockreceiveany), [receiveuntil](#tcpsockreceiveuntil), and [send](#tcpsocksend) methods.

When the `raw` argument is `true`, it is required that no pending data from any previous [ngx.say](#ngxsay), [ngx.print](#ngxprint), or [ngx.send_headers](#ngxsend_headers) calls exists. So if you have these downstream output calls previously, you should call [ngx.flush(true)](#ngxflush) before calling `ngx.req.socket(true)` to ensure that there is no pending output data. If the request body has not been read yet, then this "raw socket" can also be used to read the request body.

Expand Down Expand Up @@ -7269,7 +7269,7 @@ See also [ngx.socket.udp](#ngxsocketudp).
tcpsock:connect
---------------

**syntax:** *ok, err = tcpsock:connect(host, port, options_table?)*
**syntax:** *ok, err = tcpsock:connect(host, port?, options_table?)*

**syntax:** *ok, err = tcpsock:connect("unix:/path/to/unix-domain.socket", options_table?)*

Expand All @@ -7288,6 +7288,8 @@ Both IP addresses and domain names can be specified as the `host` argument. In c

If the nameserver returns multiple IP addresses for the host name, this method will pick up one randomly.

`port` is optional and accept nil (for compatibility with host is a unix domain), and it default value is zero.

In case of error, the method returns `nil` followed by a string describing the error. In case of success, the method returns `1`.

Here is an example for connecting to a TCP server:
Expand Down
8 changes: 5 additions & 3 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -4342,7 +4342,7 @@ See also [[#ngx.req.init_body|ngx.req.init_body]].

'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''

Returns a read-only cosocket object that wraps the downstream connection. Only [[#tcpsock:receive|receive]] and [[#tcpsock:receiveuntil|receiveuntil]] methods are supported on this object.
Returns a read-only cosocket object that wraps the downstream connection. Only [[#tcpsock:receive|receive]], [[#tcpsock:receiveany|receiveany]] and [[#tcpsock:receiveuntil|receiveuntil]] methods are supported on this object.

In case of error, <code>nil</code> will be returned as well as a string describing the error.

Expand All @@ -4351,7 +4351,7 @@ The socket object returned by this method is usually used to read the current re
If any request body data has been pre-read into the Nginx core request header buffer, the resulting cosocket object will take care of this to avoid potential data loss resulting from such pre-reading.
Chunked request bodies are not yet supported in this API.

Since the <code>v0.9.0</code> release, this function accepts an optional boolean <code>raw</code> argument. When this argument is <code>true</code>, this function returns a full-duplex cosocket object wrapping around the raw downstream connection socket, upon which you can call the [[#tcpsock:receive|receive]], [[#tcpsock:receiveuntil|receiveuntil]], and [[#tcpsock:send|send]] methods.
Since the <code>v0.9.0</code> release, this function accepts an optional boolean <code>raw</code> argument. When this argument is <code>true</code>, this function returns a full-duplex cosocket object wrapping around the raw downstream connection socket, upon which you can call the [[#tcpsock:receive|receive]], [[#tcpsock:receiveany|receiveany]], [[#tcpsock:receiveuntil|receiveuntil]], and [[#tcpsock:send|send]] methods.

When the <code>raw</code> argument is <code>true</code>, it is required that no pending data from any previous [[#ngx.say|ngx.say]], [[#ngx.print|ngx.print]], or [[#ngx.send_headers|ngx.send_headers]] calls exists. So if you have these downstream output calls previously, you should call [[#ngx.flush|ngx.flush(true)]] before calling <code>ngx.req.socket(true)</code> to ensure that there is no pending output data. If the request body has not been read yet, then this "raw socket" can also be used to read the request body.

Expand Down Expand Up @@ -6157,7 +6157,7 @@ See also [[#ngx.socket.udp|ngx.socket.udp]].

== tcpsock:connect ==

'''syntax:''' ''ok, err = tcpsock:connect(host, port, options_table?)''
'''syntax:''' ''ok, err = tcpsock:connect(host, port?, options_table?)''

'''syntax:''' ''ok, err = tcpsock:connect("unix:/path/to/unix-domain.socket", options_table?)''

Expand All @@ -6175,6 +6175,8 @@ Both IP addresses and domain names can be specified as the <code>host</code> arg

If the nameserver returns multiple IP addresses for the host name, this method will pick up one randomly.

<code>port</code> is optional and accept nil (for compatibility with host is a unix domain), and it default value is zero.

In case of error, the method returns <code>nil</code> followed by a string describing the error. In case of success, the method returns <code>1</code>.

Here is an example for connecting to a TCP server:
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ngx_http_lua_escape_args(u_char *dst, u_char *src, size_t size)
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
n++;
}

src++;
size--;
}
Expand All @@ -72,6 +73,7 @@ ngx_http_lua_escape_args(u_char *dst, u_char *src, size_t size)
} else {
*dst++ = *src++;
}

size--;
}

Expand Down
9 changes: 9 additions & 0 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ typedef struct {
#endif


/**
* max positive +1.7976931348623158e+308
* min positive +2.2250738585072014e-308
*/
#ifndef NGX_DOUBLE_LEN
#define NGX_DOUBLE_LEN 25
#endif


#if (NGX_PCRE)
#include <pcre.h>
# if (PCRE_MAJOR > 8) || (PCRE_MAJOR == 8 && PCRE_MINOR >= 21)
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_directive.c
Original file line number Diff line number Diff line change
Expand Up @@ -1378,13 +1378,15 @@ ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd)
if (dst == NULL) {
return NGX_CONF_ERROR;
}

dst->len = len;
dst->len--; /* skip the trailing '}' block terminator */

p = ngx_palloc(cf->pool, len);
if (p == NULL) {
return NGX_CONF_ERROR;
}

dst->data = p;

for (i = 0; i < cf->args->nelts; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_lua_headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
} else {
lua_pushliteral(L, "close");
}

lua_rawset(L, -3);

if (r->chunked) {
Expand Down
3 changes: 3 additions & 0 deletions src/ngx_http_lua_headers_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ ngx_http_lua_validate_host(ngx_str_t *host, ngx_pool_t *pool, ngx_uint_t alloc)
if (dot_pos == i - 1) {
return NGX_DECLINED;
}

dot_pos = i;
break;

Expand Down Expand Up @@ -754,6 +755,7 @@ ngx_http_lua_rm_header_helper(ngx_list_t *l, ngx_list_part_t *cur,
if (part->next == NULL) {
return NGX_ERROR;
}

part = part->next;
}

Expand Down Expand Up @@ -798,6 +800,7 @@ ngx_http_lua_rm_header_helper(ngx_list_t *l, ngx_list_part_t *cur,
if (part->next == NULL) {
return NGX_ERROR;
}

part = part->next;
}

Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_lua_initworkerby.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
if (part->next == NULL) {
break;
}

part = part->next;
ofile = part->elts;
i = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/ngx_http_lua_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ngx_http_lua_log.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_log_ringbuf.h"
#include "ngx_http_lua_output.h"


static int ngx_http_lua_print(lua_State *L);
Expand Down Expand Up @@ -121,6 +122,7 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
if (*p == '/' || *p == '\\') {
name.data = p + 1;
}

p++;
}

Expand All @@ -142,6 +144,9 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
type = lua_type(L, i);
switch (type) {
case LUA_TNUMBER:
size += ngx_http_lua_get_num_len(L, i);
break;

case LUA_TSTRING:
lua_tolstring(L, i, &len);
size += len;
Expand Down Expand Up @@ -210,6 +215,9 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
type = lua_type(L, i);
switch (type) {
case LUA_TNUMBER:
p = ngx_http_lua_write_num(L, i, p);
break;

case LUA_TSTRING:
q = (u_char *) lua_tolstring(L, i, &len);
p = ngx_copy(p, q, len);
Expand Down
13 changes: 12 additions & 1 deletion src/ngx_http_lua_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)

switch (type) {
case LUA_TNUMBER:
size += ngx_http_lua_get_num_len(L, i);
break;

case LUA_TSTRING:

lua_tolstring(L, i, &len);
Expand Down Expand Up @@ -173,6 +176,9 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
type = lua_type(L, i);
switch (type) {
case LUA_TNUMBER:
b->last = ngx_http_lua_write_num(L, i, b->last);
break;

case LUA_TSTRING:
p = lua_tolstring(L, i, &len);
b->last = ngx_copy(b->last, (u_char *) p, len);
Expand Down Expand Up @@ -301,8 +307,10 @@ ngx_http_lua_calc_strlen_in_table(lua_State *L, int index, int arg_i,

switch (type) {
case LUA_TNUMBER:
case LUA_TSTRING:
size += ngx_http_lua_get_num_len(L, -1);
break;

case LUA_TSTRING:
lua_tolstring(L, -1, &len);
size += len;
break;
Expand Down Expand Up @@ -396,6 +404,9 @@ ngx_http_lua_copy_str_in_table(lua_State *L, int index, u_char *dst)
type = lua_type(L, -1);
switch (type) {
case LUA_TNUMBER:
dst = ngx_http_lua_write_num(L, -1, dst);
break;

case LUA_TSTRING:
p = (u_char *) lua_tolstring(L, -1, &len);
dst = ngx_copy(dst, p, len);
Expand Down
44 changes: 44 additions & 0 deletions src/ngx_http_lua_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,50 @@ ngx_int_t ngx_http_lua_flush_resume_helper(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);


/* Get the maximum possible length, not the actual length */
static ngx_inline size_t
ngx_http_lua_get_num_len(lua_State *L, int idx)
{
double num;

num = (double) lua_tonumber(L, idx);
if (num == (double) (long) num) {
return NGX_INT64_LEN;
}

return NGX_DOUBLE_LEN;
}


static ngx_inline u_char *
ngx_http_lua_write_num(lua_State *L, int idx, u_char *dst)
{
double num;
int n;

num = (double) lua_tonumber(L, idx);
if (num == (double) (long) num) {
dst = ngx_snprintf(dst, NGX_INT64_LEN, "%l", (long) num);

} else {
/**
* The maximum number of significant digits is 14 in lua.
* Please refer to lj_strfmt.c for more details.
*/
n = snprintf((char *) dst, NGX_DOUBLE_LEN, "%.14g", num);
if (n < 0) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"snprintf(\"%f\") failed");

} else {
dst += n;
}
}

return dst;
}


#endif /* _NGX_HTTP_LUA_OUTPUT_H_INCLUDED_ */

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
3 changes: 3 additions & 0 deletions src/ngx_http_lua_req_body.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ ngx_http_lua_ngx_req_set_body_data(lua_State *L)
if (b->start == NULL) {
return luaL_error(L, "no memory");
}

b->end = b->start + body.len;

b->pos = b->start;
Expand All @@ -454,6 +455,7 @@ ngx_http_lua_ngx_req_set_body_data(lua_State *L)
if (rb->bufs == NULL) {
return luaL_error(L, "no memory");
}

rb->bufs->next = NULL;

b = ngx_create_temp_buf(r->pool, body.len);
Expand Down Expand Up @@ -899,6 +901,7 @@ ngx_http_lua_ngx_req_set_body_file(lua_State *L)
if (rb->bufs == NULL) {
return luaL_error(L, "no memory");
}

rb->bufs->next = NULL;

b = ngx_calloc_buf(r->pool);
Expand Down
Loading