URL scanning

URL scanning — Find URLs and email addresses in UTF-8 text

Functions

gboolean g_mime_url_scan ()

Types and Values

Description

The stateless URL scanner recognizes explicit URI schemes, bare www. and ftp. hostnames, and email addresses. Matches use UTF-8 byte offsets and include a prefix for constructing link targets. This is the same scanner used by GMimeTextToHtml and GMimeFlowedToHtml.

Scanning detects links in prose; it does not validate arbitrary URIs or determine whether a link is safe to open. Applications must apply their own scheme policy and escape link targets and visible text when generating HTML.

Functions

g_mime_url_scan ()

gboolean
g_mime_url_scan (const char *text,
                 gsize start,
                 gsize length,
                 GMimeUrlMatch *match);

Finds the next URL or email address in the specified range. The range must contain valid UTF-8 and begin and end on character boundaries; UTF-8 is not validated by this function. The buffer must contain at least start + length bytes, but need not be NUL-terminated. Embedded NUL bytes act as separators rather than terminating the scan.

Match offsets are relative to text , not start , and the end offset is exclusive. To find subsequent matches, resume at the previous match's end offset and reduce length accordingly. No bytes outside the specified range are examined. On failure to find a match, match is left unchanged.

Recognized schemes are mailto:, telnet://, webcal://, https://, callto:, file://, sftp://, http://, news://, nntp://, h323:, ftp://, and sip:. Scheme matching is case-insensitive. Bare www. and ftp. names and email addresses are also recognized. Surrounding punctuation is excluded when possible; matching is intended for links in prose, not URI validation.

Parameters

text

a UTF-8 text buffer

 

start

the byte offset at which to begin scanning

 

length

the number of bytes to scan, starting at start

 

match

storage for the match.

[out caller-allocates]

Returns

TRUE if a match was found, or FALSE otherwise.

Types and Values

GMimeUrlMatch

typedef struct {
	gsize start;
	gsize end;
	const char *prefix;
} GMimeUrlMatch;

A URL or email address found by g_mime_url_scan(). Prepend prefix to the matched text to form the link target. The prefix is "http://" for bare www. names, "ftp://" for bare ftp. names, and "mailto:" for email addresses. The match does not own any memory and requires no cleanup.

Members

gsize start;

the inclusive byte offset of the match relative to the input buffer

 

gsize end;

the exclusive byte offset of the match relative to the input buffer

 

const char *prefix;

a borrowed, static URI prefix, or an empty string for an explicit scheme