๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ชฉ๋ก์ด ์—†์Šต๋‹ˆ๋‹ค.

[MySQL/MariaDB] URL Decoder ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ

๐Ÿ›ข DB

    ํด๋ผ์ด์–ธํŠธ ์š”๊ตฌ์‚ฌํ•ญ

    ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ ์ค‘์— ์„œ๋น„์Šค ํŽ˜์ด์ง€์— ์ ‘๊ทผ๋˜๋Š” ์‚ฌ์šฉ์ž๋“ค์˜ ์•„์ดํ”ผ, ์–ด๋–ค ๋ธŒ๋ผ์šฐ์ €, ๋ชจ๋ฐ”์ผ์ธ์ง€ PC์ธ์ง€, ์–ด๋–ค ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋˜์ง€๋Š”์ง€๋ฅผ DB๋กœ ์ €์žฅํ•ด์„œ ๋ณด๊ณ ์‹ถ์–ดํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ querystring์œผ๋กœ ๊ตฌ์„ฑํ•˜๋ฉด URL๋กœ ๊ตฌ์„ฑ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์ฝ”๋”ฉ์„ ํ•ด์ฃผ์–ด์•ผ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋”ฐ๋ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์„ ๋ฏธ๋ฆฌ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ์˜ˆ๋ฐฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

    ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ์ธ์ฝ”๋”ฉ ๋œ ์ƒํƒœ๋กœ DB์— ์ €์žฅ์ด๋˜๋ฉด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๊ณ  ์‹ถ๋˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ด๋–ค ๊ฐ’์ธ์ง€ ์•Œ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

    ์ด ๋•Œ ์ฟผ๋ฆฌ๋ฅผ ์กฐํšŒํ•ด์˜ฌ ๋•Œ URL Decoder ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋กœ ๋””์ฝ”๋”ฉ์„ ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    MySQL / MariaDB URL Decoder ํ•จ์ˆ˜

    DELIMITER |
    
    CREATE FUNCTION urldecode (s VARCHAR(4096)) RETURNS VARCHAR(4096)
    DETERMINISTIC 
    CONTAINS SQL 
    BEGIN
           DECLARE c VARCHAR(4096) DEFAULT '';
           DECLARE pointer INT DEFAULT 1;
           DECLARE s2 VARCHAR(4096) DEFAULT '';
           DECLARE h3 VARCHAR(4096) DEFAULT '';
    
           IF ISNULL(s) THEN
              RETURN NULL;
           ELSE
           SET s2 = '';
           WHILE pointer <= LENGTH(s) DO
              SET c = SUBSTR(s,pointer,1);
              IF c = '+' THEN
                 SET h3 = '20';
              ELSEIF c = '%' AND pointer + 2 <= LENGTH(s) THEN
                       SET h3 = UPPER(SUBSTR(s,pointer+1,2));
                       SET pointer = pointer + 2;
              ELSE
                       SET h3 = HEX(c);
              END IF;
              SET s2 = CONCAT(s2,h3);
              SET pointer = pointer + 1;
           END while;
           END IF;
           RETURN UNHEX(s2);
    END;
     
    |
    
    DELIMITER ;

     

    • ํ•จ์ˆ˜ ์ƒ์„ฑ ์ฟผ๋ฆฌ๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ˜„์žฌ ์ ‘์†ํ•œ ๊ณ„์ •์— ํ•จ์ˆ˜ ์ƒ์„ฑ ๊ถŒํ•œ์ด ์žˆ์–ด์•ผํ•œ๋‹ค.
    • ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์„ค์ •์ด DB์„œ๋ฒ„์— ์…‹ํŒ…๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ log_bin_trust_function_creators ๊ฐ’์ด ์žˆ๋‹ค.

     

    ์‚ฌ์šฉ๋ฐฉ๋ฒ•

    SELECT urldecode('์ธ์ฝ”๋”ฉ๋œ๋ฌธ์ž');

     

    ์—๋Ÿฌ

    ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋‹ˆ๊นŒ ํŠน์ • ๋‹จ์–ด๋Š” decode ์‹œ hex ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์•„์ง ์›์ธ ํŒŒ์•… ๋ฐ ํ•ด๊ฒฐ์€ ํ•˜์ง€ ๋ชปํ•˜์˜€์Œ.

     

     

     

    ์ฐธ๊ณ 

    ๋„ค์ด๋ฒ„๋ธ”๋กœ๊ทธ