Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url host hotnamame and hash password and userna #46026

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions packages/react-native/Libraries/Blob/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,29 @@
import type Blob from './Blob';

import NativeBlobModule from './NativeBlobModule';

import {match} from "hermes-transform/dist/traverse/esquery";
import {tsThisType} from "@babel/types";
function getDefaultPort(protocol) {
switch (protocol) {
case 'http':
return 80;
case 'https':
return 443;
case 'ftp':
return 21;
case 'smtp':
return 25;
case 'pop3':
return 110;
case 'imap':
return 143;
case 'mysql':
return 3306;
// Add more protocols and their default ports as needed
default:
return null; // Unknown protocol
}
}
let BLOB_URL_PREFIX = null;

if (
Expand Down Expand Up @@ -81,7 +103,7 @@ export class URL {
let baseUrl = null;
if (!base || validateBaseUrl(url)) {
this._url = url;
if (!this._url.endsWith('/')) {
if (!this._url.endsWith('/') && this.search==="") {
this._url += '/';
}
} else {
Expand All @@ -107,43 +129,48 @@ export class URL {
}

get hash(): string {
throw new Error('URL.hash is not implemented');
return this.href.match(/#(.*)$/g)?this.href.match(/#(.*)$/g)[0]:""
}

get host(): string {
throw new Error('URL.host is not implemented');
return this.hostname+(this.port!==""?(":"+this.port):"")
}

get hostname(): string {
throw new Error('URL.hostname is not implemented');
return this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/(?:[^@]*@)?([^\/:]+)/)?this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/(?:[^@]*@)?([^\/:]+)/)[1]:""
}

get href(): string {
return this.toString();
}

get origin(): string {
throw new Error('URL.origin is not implemented');
return this.protocol+"://"+this.host

}

get password(): string {
throw new Error('URL.password is not implemented');
return this.href.match( /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^:]+:([^@]+)@/)?this.href.match( /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^:]+:([^@]+)@/)[1]:"";
}

get pathname(): string {
throw new Error('URL.pathname not implemented');
return this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^\/]+(\/[^?#]*)/)?this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/[^\/]+(\/[^?#]*)/)[1]:""
}

get port(): string {
throw new Error('URL.port is not implemented');
let match = this.href.match(/:[0-9]+/g)
if (match===null || parseInt(match[0].slice(1))===getDefaultPort(this.protocol)){
return ""
}
return match[0].slice(1)
}

get protocol(): string {
throw new Error('URL.protocol is not implemented');
return this.href.match(/^([a-z]+):/g)?this.href.match(/^([a-z]+):/g)[0].slice(0,-1):""
}

get search(): string {
throw new Error('URL.search is not implemented');
return this.href.match(/\?(.)+$/g)?this.href.match(/\?(.)+$/g)[0]:"";
}

get searchParams(): URLSearchParams {
Expand All @@ -168,6 +195,6 @@ export class URL {
}

get username(): string {
throw new Error('URL.username is not implemented');
return this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/([^:]+)@/)?this.href.match(/^[a-zA-Z][a-zA-Z\d+\-.]*:\/\/([^:]+)@/)[1]:"";
}
}
33 changes: 33 additions & 0 deletions packages/react-native/Libraries/Blob/__tests__/URL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,37 @@ describe('URL', function () {
const k = new URL('en-US/docs', 'https://developer.mozilla.org');
expect(k.href).toBe('https://developer.mozilla.org/en-US/docs');
});
it("hash",()=>{
const l = new URL("https://developer.mozilla.org");
// expect(l.hostname).toBe("developer.mozilla.org")
const m = new URL('en-US/docs/#end',l);
expect(m.href).toBe("https://developer.mozilla.org/en-US/docs/#end")
expect(m.hash).toBe("#end")
})
it("host is a hostname and port",()=>{
// The port is not provided
const httpPort = "123";
const protocol = "https"
const hostname = 'developer.mozilla.org'
const pathName = '/en-US/docs/Web/API/URL/host'
const n = new URL(`${protocol}://${hostname}:${httpPort}${pathName}`);
expect(n.hostname).toBe(`${hostname}`)
expect(n.origin).toBe(`${protocol}://${hostname}:${httpPort}`)
// the 443 is default https
const httpPort2 = "443";
const r = new URL(`${protocol}://${hostname}:${httpPort2}${pathName}`);
expect(r.protocol).toBe(protocol)
expect(r.port).toBe("")
expect(r.host).toBe("developer.mozilla.org")
// the port is other
const p = new URL("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host");
expect(p.host).toBe("developer.mozilla.org:4097")
expect(p.pathname).toBe("/en-US/docs/Web/API/URL/host/")
const q = new URL("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host?q=dad");
expect(q.search).toBe("?q=dad")
expect(q.href).toBe("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host?q=dad")
let u = new URL("https://username:password@developer.mozilla.org:443/en-US/docs/Web/API/URL/host")
expect(u.host).toBe("developer.mozilla.org")
expect(u.password).toBe("password")
})
});