免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1359 | 回复: 1
打印 上一主题 下一主题

[C] 在网上看了一段代码,有点糊涂,希望好心的cuer帮我指明一条思路 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-22 10:14 |只看该作者 |倒序浏览
在网上看了一段代码,有点糊涂,希望好心的cu帮我指明一条思路,源码来自于:http://www.monkey.org/~provos/crawl/

1.这里是调用部分
http_register_dispatch("image/", image_saver);
http_register_dispatch("audio/", image_saver);
http_register_dispatch("video/", image_saver);

2.http_register_dispatch
http_register_dispatch(char *type, int (*cb)(struct uri *))
{
        struct dispatch *dispatch;

        dispatch = malloc(sizeof (struct dispatch));
        if (dispatch == NULL) {
                warn("%s: malloc", __func__);
                return (-1);
        }
        dispatch->type = type;
        dispatch->cb = cb;

        TAILQ_INSERT_TAIL(&dispatchqueue, dispatch, next);

        return (0);
}

int image_saver(struct uri *uri)
{
        struct stat sb;
        char *path;
        char tmp[128], *p = "";
        char *url;
         这里不是很理解,http_register_dispatch("image/", image_saver)调用的时候明明传递的是"image/"
        url = http_make_url(&uri->url);

        /*
         * Sometimes we download something that should have been
         * html, but is media instead.
         */
        if (uri->save_fd == -1 && !img_permitted(url))
                return (-1);

        if (uri->length != -1 && uri->bdlen != uri->length) {
                snprintf(tmp, sizeof(tmp), " (%4.1f%%/%d)",
                    (float)uri->bdlen/uri->length*100, uri->length);
                p = tmp;
        }
        fprintf(stdout, "%s %s%s\n",
                uri->flags & HTTP_REQUEST_GET ? "GET" : "HEAD",
                url, uri->flags & HTTP_REQUEST_GET ? p : "");

        if (uri->flags & HTTP_REQUEST_HEAD) {
                int minlen, maxlen;

                if (uri->format == NULL) {
                        minlen = media_minlen;
                        maxlen = media_maxlen;
                } else {
                        /* Get lengths depending on mime types */
                        minlen = conf_get_num(uri->format, "Min-Length",
                            media_minlen);
                        maxlen = conf_get_num(uri->format, "Max-Length",
                            media_maxlen);
                }

                /* See if it meets our extra constraints */
                if ((minlen != -1 && uri->length < minlen) ||
                    (maxlen != -1 && uri->length > maxlen))
                        return (-1);

                /* Re-add request as GET */
                http_add(HTTP_REQUEST_GET, url, uri->depth);
                return (0);
        }

        if ((path = construct_path(url, 1)) == NULL)
                return (-1);
        if (stat(path, &sb) != -1) {
                if (sb.st_size >= uri->length)
                        return (-1);
        }
        if (uri->save_fd == -1) {
                uri->save_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
                if (uri->save_fd == -1)
                        return (-1);
        }

        if (atomicio(write, uri->save_fd, uri->body, uri->bdread) == -1)
                return (-1);

        uri->bdread = 0;
       
        return (0);
}


http_make_url(struct url *url)
{
        这一步数据不知道怎么来的了
        static char output[1024];

        if (url->port != HTTP_DEFAULTPORT)
                snprintf(output, sizeof(output), "http://%s:%d%s",
                    url->host, url->port, url->file);
        else
                snprintf(output, sizeof(output), "http://%s%s",
                    url->host, url->file);

        return (output);
}

最终我想知道,传递的参数是image..等类型,url是怎么生成的?

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2012-10-22 11:18 |只看该作者
这程序的意思应该是爬虫框架根据Content-Type猜测出数据类型, 然后调用你注册的callback.

在callback里, 如果回调给你的uri的flag是HEAD命令(HEAD uri HTTP/1.1), 那么你uri里已经装好了为你HEAD探测来的内容大小, 你就根据配置中指定的希望获取的文件大小范围确定是否要下载这个文件, 一旦确定在范围内就开始下载.

下一次callback回调该uri的时候, 就是已经为你准备好连接fd了, 你直接读fd写到文件里就完事了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP