Пример #1
0
int lua_header ( lua_State *L )
{
    if ( !lua_isuserdata ( L, 1 ) ) {
        luaL_error ( L, "miss epd!" );
        return 0;
    }

    epdata_t *epd = lua_touserdata ( L, 1 );

    if ( lua_gettop ( L ) < 2 ) {
        return 0;
    }

    int t = lua_type ( L, 2 );
    size_t dlen = 0;
    const char *data = NULL;

    if ( t == LUA_TSTRING ) {
        data = lua_tolstring ( L, 2, &dlen );

        if ( stristr ( data, "content-length", dlen ) != data ) {
            network_send_header ( epd, data );
        }

    } else if ( t == LUA_TTABLE ) {
        int len = lua_objlen ( L, 2 ), i = 0;

        for ( i = 0; i < len; i++ ) {
            lua_pushinteger ( L, i + 1 );
            lua_gettable ( L, -2 );

            if ( lua_isstring ( L, -1 ) ) {
                data = lua_tolstring ( L, -1, &dlen );

                if ( stristr ( data, "content-length", dlen ) != data ) {
                    network_send_header ( epd, lua_tostring ( L, -1 ) );
                }
            }

            lua_pop ( L, 1 );
        }
    }

    return 0;
}
Пример #2
0
int lua_header(lua_State *L)
{
    epdata_t *epd = get_epd(L);

    if(!epd) {
        lua_pushnil(L);
        lua_pushstring(L, "miss epd!");
        return 2;
    }

    if(epd->websocket) {
        return 0;
    }

    if(lua_gettop(L) < 1) {
        return 0;
    }

    if(epd->header_sended != 0) {
        lua_pushnil(L);
        lua_pushstring(L, "respone header has been sended");
        return 2;
    }

    int t = lua_type(L, 1);
    size_t dlen = 0;
    const char *data = NULL;
    int ret = 0;

    if(t == LUA_TSTRING) {
        data = lua_tolstring(L, 1, &dlen);

        if(stristr(data, "content-length", dlen) != data) {
            ret = network_send_header(epd, data);
        }

    } else if(t == LUA_TTABLE) {
        int len = lua_objlen(L, 1), i = 0;

        for(i = 0; i < len; i++) {
            lua_pushinteger(L, i + 1);
            lua_gettable(L, -2);

            if(lua_isstring(L, -1)) {
                data = lua_tolstring(L, -1, &dlen);

                if(stristr(data, "content-length", dlen) != data) {
                    ret = network_send_header(epd, lua_tostring(L, -1));
                }
            }

            lua_pop(L, 1);
        }
    }

    if(ret == -1) {
        lua_pushnil(L);
        lua_pushstring(L, "respone header too big");
        return 2;

    } else if(ret == 0) {
        lua_pushnil(L);

    } else {
        lua_pushboolean(L, 1);
    }

    return 1;
}
Пример #3
0
int network_sendfile(epdata_t *epd, const char *path)
{
    if(epd->process_timeout == 1) {
        return 0;
    }

    struct stat st;

    if((epd->response_sendfile_fd = open(path, O_RDONLY)) < 0) {
        epd->response_sendfile_fd = -2;
        //printf ( "Can't open '%s' file\n", path );
        return 0;
    }

    if(fstat(epd->response_sendfile_fd, &st) == -1) {
        close(epd->response_sendfile_fd);
        epd->response_sendfile_fd = -2;
        //printf ( "Can't stat '%s' file\n", path );
        return 0;
    }

    epd->response_content_length = st.st_size;
    epd->response_buf_sended = 0;

    /// clear send bufs;!!!
    int i = 0;

    for(i = 1; i < epd->iov_buf_count; i++) {
        free(epd->iov[i].iov_base);
        epd->iov[i].iov_base = NULL;
        epd->iov[i].iov_len = 0;
    }

    epd->iov_buf_count = 0;

    struct tm *_clock;
    _clock = gmtime(&(st.st_mtime));
    sprintf(_gmt_time, "%s, %02d %s %04d %02d:%02d:%02d GMT",
            DAYS_OF_WEEK[_clock->tm_wday],
            _clock->tm_mday,
            MONTHS_OF_YEAR[_clock->tm_mon],
            _clock->tm_year + 1900,
            _clock->tm_hour,
            _clock->tm_min,
            _clock->tm_sec);

    if(epd->if_modified_since && strcmp(_gmt_time, epd->if_modified_since) == 0) {
        epd->response_header_length = 0;
        free(epd->iov[0].iov_base);
        epd->iov[0].iov_base = NULL;
        epd->iov[0].iov_len = 0;
        network_send_header(epd, "HTTP/1.1 304 Not Modified");
        close(epd->response_sendfile_fd);
        epd->response_sendfile_fd = -1;
        epd->response_content_length = 0;
        return 1;
    }

    if(epd->iov[0].iov_base == NULL || !stristr(epd->iov[0].iov_base, "content-type:", epd->response_header_length)) {
        sprintf(temp_buf, "Content-Type: %s", get_mime_type(path));
        network_send_header(epd, temp_buf);
    }

    sprintf(temp_buf, "Last-Modified: %s", _gmt_time);
    network_send_header(epd, temp_buf);

    if(temp_buf[14] == 't' && temp_buf[15] == 'e') {
        int fd = epd->response_sendfile_fd;
        epd->response_sendfile_fd = -1;
        epd->response_content_length = 0;
        int n = 0;

        while((n = read(fd, &temp_buf, 4096)) > 0) {
            network_send(epd, temp_buf, n);
        }

        if(n < 0) {
        }

        close(fd);

        return 1;
    }

    /*
    #ifdef linux
        int set = 1;
        setsockopt(epd->fd, IPPROTO_TCP, TCP_CORK, &set, sizeof(int));
    #endif
    */
    return 1;
}