Example #1
0
def redis_callback(t):
    req = t.get_req()
    resp = t.get_resp()
    state = t.get_state()
    error = t.get_error()

    if state != wf.WFT_STATE_SUCCESS:
        print("state:{} error:{} errstr:{}".format(
            state, error, wf.get_error_string(state, error)))
        return
    else:
        val = resp.get_result()
        if val.is_error():
            print("Error reply. Need a password?")
            return

    cmd = req.get_command()
    if cmd == "SET":
        user_data = t.get_user_data()
        n = wf.create_redis_task(user_data.url,
                                 retry_max=2,
                                 callback=redis_callback)
        n.get_req().set_request("GET", [user_data.key])
        wf.series_of(t).push_back(n)
        print("Redis SET request success. Trying to GET...")
    else:
        if val.is_string():
            print("Redis GET success. Value = {}".format(val.string_value()))
        else:
            print("Error. Not a string value")

        print("Finish")
Example #2
0
def mysql_callback(task):
    state = task.get_state()
    error = task.get_error()

    if state != wf.WFT_STATE_SUCCESS:
        print(wf.get_error_string(state, error))
        return

    resp = task.get_resp()
    cursor = wf.MySQLResultCursor(resp)

    for result_set in wf.MySQLResultSetIterator(cursor):
        if result_set.get_cursor_status() == wf.MYSQL_STATUS_GET_RESULT:
            fields = result_set.fetch_fields()

            print(header_line(len(fields)))
            print(format_row(fields))
            print(header_line(len(fields)))

            for row in wf.MySQLRowIterator(result_set):
                print(format_row(row))

            print(header_line(len(fields)))
            print("{} {} in set\n".format(
                result_set.get_rows_count(),
                "row" if result_set.get_rows_count() == 1 else "rows"))
        elif result_set.get_cursor_status() == wf.MYSQL_STATUS_OK:
            print("OK. {} {} affected. {} warnings. insert_id={}. {}".format(
                result_set.get_affected_rows(),
                "row" if result_set.get_affected_rows() == 1 else "rows",
                result_set.get_warnings(), result_set.get_insert_id(),
                as_str(result_set.get_info())))

    if resp.get_packet_type() == wf.MYSQL_PACKET_OK:
        print("OK. {} {} affected. {} warnings. insert_id={}. {}".format(
            resp.get_affected_rows(),
            "row" if resp.get_affected_rows() == 1 else "rows",
            resp.get_warnings(), resp.get_last_insert_id(),
            as_str(resp.get_info())))

    elif resp.get_packet_type() == wf.MYSQL_PACKET_ERROR:
        print("ERROR. error_code={} {}".format(resp.get_error_code(),
                                               as_str(resp.get_error_msg())))

    url = wf.series_of(task).get_context()
    next_task = create_next_task(url, mysql_callback)
    if next_task is not None:
        wf.series_of(task).push_back(next_task)
    pass
Example #3
0
def http_callback(t):
    state = t.get_state()
    error = t.get_error()
    resp = t.get_resp()
    series = wf.series_of(t)
    ctx = series.get_context()
    proxy_resp = ctx.proxy_task.get_resp()

    if state == wf.WFT_STATE_SUCCESS:
        ctx.proxy_task.set_callback(reply_callback)
        # move content from resp to proxy_resp, then you cannot use resp
        resp.move_to(proxy_resp)
        if not ctx.is_keep_alive:
            proxy_resp.set_header_pair("Connection", "close")
    else:
        errstr = ""
        if state == wf.WFT_STATE_SYS_ERROR:
            errstr = "system error: {}".format(os.strerror(error))
        elif state == wf.WFT_STATE_DNS_ERROR:
            errstr = "DNS error: {}".format(error)
        elif state == wf.WFT_STATE_SSL_ERROR:
            errstr = "SSL error: {}".format(error)
        else:
            errstr = "URL error (Cannot be a HTTPS proxy)"
        print("{} Fetch failed, state:{} error:{} {}".format(
            ctx.url, state, error, errstr))
        proxy_resp.set_status_code("404")
        proxy_resp.append_body(b"<html>404 Not Found.</html>")
def http_callback(t):
    req = t.get_req()
    resp = t.get_resp()
    state = t.get_state()
    error = t.get_error()

    if state != wf.WFT_STATE_SUCCESS:
        print("http: state:{} error:{} errstr:{}".format(
            state, error, wf.get_error_string(state, error)))
        return

    body_len = resp.get_body_size()
    if body_len == 0:
        print("Error: empty Http body!")
        return

    series = wf.series_of(t)
    context = series.get_context()
    context.body_len = body_len
    redis_url = context.redis_url
    redis_task = wf.create_redis_task(redis_url,
                                      retry_max=2,
                                      callback=redis_callback)
    redis_task.get_req().set_request(
        "SET", [context.http_url, resp.get_body()])
    series.push_back(redis_task)
Example #5
0
def reply_callback(proxy_task):
    series = wf.series_of(proxy_task)
    ctx = series.get_context()
    proxy_resp = proxy_task.get_resp()
    sz = proxy_resp.get_body_size()
    if proxy_task.get_state() == wf.WFT_STATE_SUCCESS:
        print("{} Success, Http Status:{} Body Length:{}".format(
            ctx.url, proxy_resp.get_status_code(), sz))
    else:
        print("{} Reply failed:{} Body Length:{}".format(
            ctx.url, os.strerror(proxy_task.get_error()), sz))
def http_callback(t):
    ctx = wf.series_of(t).get_context()
    ctx.state = t.get_state()
    ctx.error = t.get_error()
    # Attention: YOU ARE NOT ALLOW TO OWN RESP AFTER THIS CALLBACK, SO MAKE A COPY
    resp = t.get_resp()
    ctx.chunked = resp.is_chunked()
    ctx.keep_alive = resp.is_keep_alive()
    ctx.status_code = resp.get_status_code()
    ctx.http_version = resp.get_http_version()
    ctx.body = resp.get_body()
Example #7
0
def process(t):
    req = t.get_req()
    series = wf.series_of(t)
    ctx = Context()
    ctx.url = req.get_request_uri()
    ctx.proxy_task = t
    series.set_context(ctx)
    ctx.is_keep_alive = req.is_keep_alive()
    http_task = wf.create_http_task(req.get_request_uri(), 0, 0, http_callback)
    req.set_request_uri(http_task.get_req().get_request_uri())
    req.move_to(http_task.get_req())
    http_task.get_resp().set_size_limit(200 * 1024 * 1024)
    series << http_task
def redis_callback(t):
    req = t.get_req()
    resp = t.get_resp()
    state = t.get_state()
    error = t.get_error()

    if state != wf.WFT_STATE_SUCCESS:
        print("redis: state:{} error:{} errstr:{}".format(
            state, error, wf.get_error_string(state, error)))
        return
    else:
        val = resp.get_result()
        if val.is_error():
            print("Error reply. Need a password?")
            return

    context = wf.series_of(t).get_context()
    print("redis SET success: key: {}, value size: {}".format(
        context.http_url, context.body_len))
    context.success = True
Example #9
0
def process(root, t):
    req = t.get_req()
    resp = t.get_resp()

    request_uri = req.get_request_uri()
    index = request_uri.find("?")
    path = root + (request_uri if index == -1 else request_uri[:index])

    if path[-1] == "/":
        path += "index.html"

    try:
        fd = os.open(path, os.O_RDONLY)
    except:
        resp.set_status_code("404")
        resp.append_body("<html>404 Not Found</html>")
        return

    series = wf.series_of(t)
    size = os.lseek(fd, 0, os.SEEK_END)
    pread_task = wf.create_pread_task(fd, size, 0, pread_callback)
    pread_task.set_user_data(resp)
    series.push_back(pread_task)