You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

url.c 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "string.h"
  2. #include "resolv.h"
  3. #include "etherboot.h" /* for arptable */
  4. #include "url.h"
  5. /*
  6. * Parse a URL and deduce a struct protocol *, a struct sockaddr_in
  7. * and a char *filename.
  8. *
  9. * We accept URLs of the form
  10. *
  11. * [protocol://[host][:port]/]path/to/file
  12. *
  13. * Returns 1 for success, 0 for failure (e.g. unknown protocol).
  14. *
  15. */
  16. int parse_url ( char *url, struct protocol **proto,
  17. struct sockaddr_in *server, char **filename ) {
  18. char *p;
  19. char *protocol = NULL;
  20. char *host = NULL;
  21. char *port = NULL;
  22. int rc = 0;
  23. DBG ( "URL parsing \"%s\"\n", url );
  24. /* If no protocol is present, the whole URL will be a filename */
  25. *filename = url;
  26. /* Search for a protocol delimiter. If found, parse out the
  27. * host and port parts of the URL, inserting NULs to terminate
  28. * the different sections.
  29. */
  30. for ( p = url ; *p ; p++ ) {
  31. if ( memcmp ( p, "://", 3 ) != 0 )
  32. continue;
  33. /* URL has an explicit protocol */
  34. *p = '\0';
  35. p += 3;
  36. protocol = url;
  37. host = p;
  38. /* Search for port and file delimiters */
  39. for ( ; *p ; p++ ) {
  40. if ( *p == ':' ) {
  41. *p = '\0';
  42. port = p + 1;
  43. continue;
  44. }
  45. if ( *p == '/' ) {
  46. *(p++) = '\0';
  47. break;
  48. }
  49. }
  50. *filename = p;
  51. break;
  52. }
  53. DBG ( "URL protocol \"%s\" host \"%s\" port \"%s\" file \"%s\"\n",
  54. protocol ? protocol : "(default)", host ? host : "(default)",
  55. port ? port : "(default)", *filename );
  56. /* Identify the protocol */
  57. *proto = identify_protocol ( protocol );
  58. if ( ! *proto ) {
  59. DBG ( "URL unknown protocol \"%s\"\n",
  60. protocol ? protocol : "(default)" );
  61. goto out;
  62. }
  63. /* Identify the host */
  64. server->sin_addr = arptable[ARP_SERVER].ipaddr;
  65. if ( host && host[0] ) {
  66. if ( ! resolv ( &server->sin_addr, host ) ) {
  67. DBG ( "URL unknown host \"%s\"\n", host );
  68. goto out;
  69. }
  70. }
  71. /* Identify the port */
  72. server->sin_port = (*proto)->default_port;
  73. if ( port && port[0] ) {
  74. server->sin_port = strtoul ( port, NULL, 10 );
  75. }
  76. rc = 1;
  77. out:
  78. /* Fill back in the original URL */
  79. if ( protocol ) {
  80. (*filename)[-1] = '/';
  81. if ( port )
  82. port[-1] = ':';
  83. host[-3] = ':';
  84. }
  85. return rc;
  86. }