瀏覽代碼

Add concept of "current working URI".

tags/v0.9.3
Michael Brown 17 年之前
父節點
當前提交
15dae1e042
共有 3 個文件被更改,包括 61 次插入5 次删除
  1. 42
    0
      src/core/cwuri.c
  2. 16
    5
      src/core/uri.c
  3. 3
    0
      src/include/gpxe/uri.h

+ 42
- 0
src/core/cwuri.c 查看文件

1
+/*
2
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <stddef.h>
20
+#include <gpxe/uri.h>
21
+
22
+/** @file
23
+ *
24
+ * Current working URI
25
+ *
26
+ * Somewhat analogous to the current working directory in a POSIX
27
+ * system.
28
+ */
29
+
30
+/** Current working URI */
31
+struct uri *cwuri = NULL;
32
+
33
+/**
34
+ * Change working URI
35
+ *
36
+ * @v uri		New working URI
37
+ */
38
+void churi ( struct uri *uri ) {
39
+	if ( cwuri )
40
+		uri_put ( cwuri );
41
+	cwuri = uri_get ( uri );
42
+}

+ 16
- 5
src/core/uri.c 查看文件

35
  * @v uri		URI
35
  * @v uri		URI
36
  */
36
  */
37
 static void dump_uri ( struct uri *uri ) {
37
 static void dump_uri ( struct uri *uri ) {
38
+	if ( ! uri )
39
+		return;
38
 	if ( uri->scheme )
40
 	if ( uri->scheme )
39
 		DBG ( " scheme \"%s\"", uri->scheme );
41
 		DBG ( " scheme \"%s\"", uri->scheme );
40
 	if ( uri->opaque )
42
 	if ( uri->opaque )
174
 /**
176
 /**
175
  * Get port from URI
177
  * Get port from URI
176
  *
178
  *
177
- * @v uri		URI
179
+ * @v uri		URI, or NULL
178
  * @v default_port	Default port to use if none specified in URI
180
  * @v default_port	Default port to use if none specified in URI
179
  * @ret port		Port
181
  * @ret port		Port
180
  */
182
  */
181
 unsigned int uri_port ( struct uri *uri, unsigned int default_port ) {
183
 unsigned int uri_port ( struct uri *uri, unsigned int default_port ) {
182
-	return ( uri->port ? strtoul ( uri->port, NULL, 0 ) : default_port );
184
+	if ( ( ! uri ) || ( ! uri->port ) )
185
+		return default_port;
186
+	return ( strtoul ( uri->port, NULL, 0 ) );
183
 }
187
 }
184
 
188
 
185
 /**
189
 /**
187
  *
191
  *
188
  * @v buf		Buffer to fill with URI string
192
  * @v buf		Buffer to fill with URI string
189
  * @v size		Size of buffer
193
  * @v size		Size of buffer
190
- * @v uri		URI to write into buffer
194
+ * @v uri		URI to write into buffer, or NULL
191
  * @ret len		Length of URI string
195
  * @ret len		Length of URI string
192
  */
196
  */
193
 int unparse_uri ( char *buf, size_t size, struct uri *uri ) {
197
 int unparse_uri ( char *buf, size_t size, struct uri *uri ) {
197
 	dump_uri ( uri );
201
 	dump_uri ( uri );
198
 	DBG ( "\n" );
202
 	DBG ( "\n" );
199
 
203
 
204
+	/* Special-case NULL URI */
205
+	if ( ! uri ) {
206
+		if ( size )
207
+			buf[0] = '\0';
208
+		return 0;
209
+	}
210
+
200
 	/* Special-case opaque URIs */
211
 	/* Special-case opaque URIs */
201
 	if ( uri->opaque ) {
212
 	if ( uri->opaque ) {
202
 		return ssnprintf ( ( buf + used ), ( size - used ),
213
 		return ssnprintf ( ( buf + used ), ( size - used ),
332
 /**
343
 /**
333
  * Resolve base+relative URI
344
  * Resolve base+relative URI
334
  *
345
  *
335
- * @v base_uri		Base URI
346
+ * @v base_uri		Base URI, or NULL
336
  * @v relative_uri	Relative URI
347
  * @v relative_uri	Relative URI
337
  * @ret resolved_uri	Resolved URI
348
  * @ret resolved_uri	Resolved URI
338
  *
349
  *
347
 	struct uri *new_uri;
358
 	struct uri *new_uri;
348
 
359
 
349
 	/* If relative URI is absolute, just re-use it */
360
 	/* If relative URI is absolute, just re-use it */
350
-	if ( uri_is_absolute ( relative_uri ) )
361
+	if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
351
 		return uri_get ( relative_uri );
362
 		return uri_get ( relative_uri );
352
 
363
 
353
 	/* Mangle URI */
364
 	/* Mangle URI */

+ 3
- 0
src/include/gpxe/uri.h 查看文件

124
 	ref_put ( &uri->refcnt );
124
 	ref_put ( &uri->refcnt );
125
 }
125
 }
126
 
126
 
127
+extern struct uri *cwuri;
128
+
127
 extern struct uri * parse_uri ( const char *uri_string );
129
 extern struct uri * parse_uri ( const char *uri_string );
128
 extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
130
 extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
129
 extern int unparse_uri ( char *buf, size_t size, struct uri *uri );
131
 extern int unparse_uri ( char *buf, size_t size, struct uri *uri );
132
 			     const char *relative_path );
134
 			     const char *relative_path );
133
 extern struct uri * resolve_uri ( struct uri *base_uri,
135
 extern struct uri * resolve_uri ( struct uri *base_uri,
134
 				  struct uri *relative_uri );
136
 				  struct uri *relative_uri );
137
+extern void churi ( struct uri *uri );
135
 
138
 
136
 #endif /* _GPXE_URI_H */
139
 #endif /* _GPXE_URI_H */

Loading…
取消
儲存