C-URL编码

发布于 2024-11-04 06:29:37 字数 72 浏览 3 评论 0原文

有没有一种简单的方法在 C 中进行 URL 编码?我正在使用 libcurl 但我还没有找到方法。具体来说,我需要进行百分比转义。

Is there a simple way to do URL encode in C? I'm using libcurl but I haven't found a method. Specifically, I need to do percent escapes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长途伴 2024-11-11 06:29:37

在C语言中基于维基百科,无需分配和释放。确保输出缓冲区至少是输入 URL 字符串的 3 倍。通常,您只需要编码最多 4K,因为 URL 往往很短,因此只需在堆栈上进行即可。

char rfc3986[256] = {0};
char html5[256] = {0};

void url_encoder_rfc_tables_init(){

    int i;

    for (i = 0; i < 256; i++){

        rfc3986[i] = isalnum( i) || i == '~' || i == '-' || i == '.' || i == '_' ? i : 0;
        html5[i] = isalnum( i) || i == '*' || i == '-' || i == '.' || i == '_' ? i : (i == ' ') ? '+' : 0;
    }
}

char *url_encode( char *table, unsigned char *s, char *enc){

    for (; *s; s++){

        if (table[*s]) *enc = table[*s];
        else sprintf( enc, "%%%02X", *s);
        while (*++enc);
    }

    return( enc);
}

像这样使用它

url_encoder_rfc_tables_init();

url_encode( html5, url, url_encoded);

In C based on wikipedia, without having to alloc and free. Make sure output buffer is at least 3X input URL string. Usually you only need to encode up to maybe 4K as URLs tend to be short so just do it on the stack.

char rfc3986[256] = {0};
char html5[256] = {0};

void url_encoder_rfc_tables_init(){

    int i;

    for (i = 0; i < 256; i++){

        rfc3986[i] = isalnum( i) || i == '~' || i == '-' || i == '.' || i == '_' ? i : 0;
        html5[i] = isalnum( i) || i == '*' || i == '-' || i == '.' || i == '_' ? i : (i == ' ') ? '+' : 0;
    }
}

char *url_encode( char *table, unsigned char *s, char *enc){

    for (; *s; s++){

        if (table[*s]) *enc = table[*s];
        else sprintf( enc, "%%%02X", *s);
        while (*++enc);
    }

    return( enc);
}

Use it like this

url_encoder_rfc_tables_init();

url_encode( html5, url, url_encoded);
苍景流年 2024-11-11 06:29:37

curl_escape

取代

显然已被curl_easy_escape

curl_escape

which apparently has been superseded by

curl_easy_escape

满身野味 2024-11-11 06:29:37

我写这个也是为了处理空格字符的查询字符串编码

用法: UrlEncode(" http://www.example.com/index.html?Hello=World", " :/", buffer, buf_size)

url:要编码的 url 字符串。可以是字符串文字或字符串数​​组

encode:要编码的以零结尾的字符串。这是很好的,因为您可以在运行时确定要编码的 url 大小

buffer:保存新字符串的缓冲区

size:缓冲区的大小

返回:如果缓冲区足够大,则返回新字符串的大小;如果缓冲区不够大,则返回所需的缓冲区大小。如果您想分配所需的确切大小,可以双击此功能。

    int UrlEncode(char* url, char* encode,  char* buffer, unsigned int size)
    {
        char chars[127] = {0};
        unsigned int length = 0;

        if(!url || !encode || !buffer) return 0;

//Create an array to hold ascii chars, loop through encode string
//and assign to place in array. I used this construct instead of a large if statement for speed.
        while(*encode) chars[*encode++] = *encode;

//Loop through url, if we find an encode char, replace with % and add hex
//as ascii chars. Move buffer up by 2 and track the length needed.
//If we reach the query string (?), move to query string encoding
    URLENCODE_BASE_URL:
        while(size && (*buffer = *url)) {
            if(*url == '?') goto URLENCODE_QUERY_STRING;
            if(chars[*url] && size > 2) {
                *buffer++ = '%';
                itoa(*url, buffer, 16);
                buffer++; size-=2; length+=2;
            }
            url++, buffer++, size--; length++;  
        }
        goto URLENCODE_RETURN;

//Same as above but on spaces (' '), replace with plus ('+') and convert
//to hex ascii. I moved this out into a separate loop for speed.
    URLENCODE_QUERY_STRING:
        while(size && (*buffer = *url)) {
            if(chars[*url] && size > 2) {
                *buffer++ = '%';
                if(*url == ' ') itoa('+', buffer, 16);
                else itoa(*url, buffer, 16);
                buffer++; size-=2; length+=2;
            }
            url++, buffer++, size--; length++;
        }

//Terminate the end of the buffer, and if the buffer wasn't large enough
//calc the rest of the url length and return
    URLENCODE_RETURN:
        *buffer = '\0';
        if(*url)
        while(*url) { if(chars[*url]) length+=2; url++; length++; }
        return length;
    }

这个函数几乎可以处理您需要的大多数(如果不是全部)url 编码。最重要的是 - 它真的很快!

I wrote this to also take care of query string encoding of the space character

Usage: UrlEncode("http://www.example.com/index.html?Hello=World", " :/", buffer, buf_size)

url: The url string to encode. Can be string literal or string array

encode: A zero-terminated string of chars to encode. This is good cause you can at runtime determine how much of the url to encode

buffer: A buffer to hold the new string

size: The size of the buffer

return: Returns the size of the new string if buffer is large enough OR returns the required buffer size if the buffer is not large enough. You can double tap this function if you want to allocate the exact size needed.

    int UrlEncode(char* url, char* encode,  char* buffer, unsigned int size)
    {
        char chars[127] = {0};
        unsigned int length = 0;

        if(!url || !encode || !buffer) return 0;

//Create an array to hold ascii chars, loop through encode string
//and assign to place in array. I used this construct instead of a large if statement for speed.
        while(*encode) chars[*encode++] = *encode;

//Loop through url, if we find an encode char, replace with % and add hex
//as ascii chars. Move buffer up by 2 and track the length needed.
//If we reach the query string (?), move to query string encoding
    URLENCODE_BASE_URL:
        while(size && (*buffer = *url)) {
            if(*url == '?') goto URLENCODE_QUERY_STRING;
            if(chars[*url] && size > 2) {
                *buffer++ = '%';
                itoa(*url, buffer, 16);
                buffer++; size-=2; length+=2;
            }
            url++, buffer++, size--; length++;  
        }
        goto URLENCODE_RETURN;

//Same as above but on spaces (' '), replace with plus ('+') and convert
//to hex ascii. I moved this out into a separate loop for speed.
    URLENCODE_QUERY_STRING:
        while(size && (*buffer = *url)) {
            if(chars[*url] && size > 2) {
                *buffer++ = '%';
                if(*url == ' ') itoa('+', buffer, 16);
                else itoa(*url, buffer, 16);
                buffer++; size-=2; length+=2;
            }
            url++, buffer++, size--; length++;
        }

//Terminate the end of the buffer, and if the buffer wasn't large enough
//calc the rest of the url length and return
    URLENCODE_RETURN:
        *buffer = '\0';
        if(*url)
        while(*url) { if(chars[*url]) length+=2; url++; length++; }
        return length;
    }

This function pretty much handles most (if not all) url encoding you'd need. Best of all - it's really fast!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文