}
if (d->http_client)
http_close(d->http_client);
+ urlreset(&d->url);
free(d->myaddr);
free(d->location);
free(d->server);
next = TAILQ_NEXT(d, disc_link);
if (d->http_client) {
if (dispatch_clock - d->http_start > 4)
- satip_discovery_destroy(d, 1);;
+ satip_discovery_destroy(d, 1);
continue;
}
d->http_client = http_connect(&d->url, NULL,
d);
if (d->http_client == NULL)
satip_discovery_destroy(d, 1);
- d->http_start = dispatch_clock;
+ else
+ d->http_start = dispatch_clock;
}
if (TAILQ_FIRST(&satip_discoveries))
gtimer_arm(&satip_discovery_timerq, satip_discovery_timerq_cb, NULL, 5);
#include <regex.h>
#include <string.h>
+
+void
+urlreset ( url_t *url )
+{
+ free(url->scheme);
+ free(url->user);
+ free(url->pass);
+ free(url->host);
+ free(url->path);
+ free(url->query);
+ free(url->frag);
+ free(url->raw);
+ memset(url, 0, sizeof(*url));
+}
+
+void
+urlcopy ( url_t *dst, const url_t *src )
+{
+ dst->scheme = strdup(src->scheme);
+ dst->user = strdup(src->user);
+ dst->pass = strdup(src->pass);
+ dst->host = strdup(src->host);
+ dst->port = src->port;
+ dst->path = strdup(src->path);
+ dst->query = strdup(src->query);
+ dst->frag = strdup(src->frag);
+ dst->raw = strdup(src->raw);
+}
+
/* Use liburiparser if available */
#if ENABLE_URIPARSER
#include <uriparser/Uri.h>
-int
+int
urlparse ( const char *str, url_t *url )
{
UriParserStateA state;
UriUriA uri;
char *s, buf[256];
+ if (str == NULL || url == NULL)
+ return -1;
+
+ urlreset(url);
+
/* Parse */
state.uri = &uri;
if (uriParseUriA(&state, str) != URI_SUCCESS) {
}
/* Store raw */
- strncpy(url->raw, str, sizeof(url->raw));
+ url->raw = strdup(str);
/* Copy */
#define uri_copy(y, x)\
+ if (x.first) {\
+ size_t len = x.afterLast - x.first;\
+ y = strndup(x.first, len);\
+ }
+#define uri_copy_static(y, x)\
if (x.first) {\
size_t len = x.afterLast - x.first;\
strncpy(y, x.first, len);\
- y[len] = 0;\
} else {\
- *y = 0;\
+ y[0] = '\0';\
}
uri_copy(url->scheme, uri.scheme);
uri_copy(url->host, uri.hostText);
uri_copy(url->user, uri.userInfo);
uri_copy(url->query, uri.query);
uri_copy(url->frag, uri.fragment);
- uri_copy(buf, uri.portText);
+ uri_copy_static(buf, uri.portText);
if (*buf)
url->port = atoi(buf);
else
url->port = 0;
- *url->path = 0;
path = uri.pathHead;
while (path) {
- strcat(url->path, "/");
uri_copy(buf, path->text);
+ url->path = realloc(url->path, strlen(url->path) + strlen(buf) + 2);
+ strcat(url->path, "/");
strcat(url->path, buf);
path = path->next;
}
if (s) {
strcpy(url->pass, s+1);
*s = 0;
- } else {
- *url->pass = 0;
}
/* Cleanup */
regmatch_t m[16];
char buf[16];
+ if (str == NULL || url == NULL)
+ return -1;
+
+ urlreset(url);
+
/* Create regexp */
if (!urlparse_exp) {
urlparse_exp = calloc(1, sizeof(regex_t));
/* Execute */
if (regexec(urlparse_exp, str, ARRAY_SIZE(m), m, 0))
- return 1;
+ return -1;
/* Extract data */
#define copy(x, i)\
+ {\
+ x = strndup(str+m[i].rm_so, m[i].rm_eo - m[i].rm_so);\
+ }(void)0
+#define copy_static(x, i)\
{\
int len = m[i].rm_eo - m[i].rm_so;\
if (len >= sizeof(x) - 1)\
copy(url->pass, 5);
copy(url->host, 6);
copy(url->path, 9);
- copy(buf, 8);
+ copy_static(buf, 8);
url->port = atoi(buf);
copy(url->query, 11);
copy(url->frag, 13);
- strncpy(url->raw, str, sizeof(url->raw));
+ url->raw = strdup(str);
return 0;
}