Show More
@@ -1148,9 +1148,10 b' def retry(func=None, exception=Exception' | |||
|
1148 | 1148 | return wrapper |
|
1149 | 1149 | |
|
1150 | 1150 | |
|
1151 | def user_agent_normalizer(user_agent_raw): | |
|
1151 | def user_agent_normalizer(user_agent_raw, safe=True): | |
|
1152 | 1152 | log = logging.getLogger('rhodecode.user_agent_normalizer') |
|
1153 | 1153 | ua = (user_agent_raw or '').strip().lower() |
|
1154 | ua = ua.replace('"', '') | |
|
1154 | 1155 | |
|
1155 | 1156 | try: |
|
1156 | 1157 | if 'mercurial/proto-1.0' in ua: |
@@ -1158,8 +1159,15 b' def user_agent_normalizer(user_agent_raw' | |||
|
1158 | 1159 | ua = ua.replace('(', '').replace(')', '').strip() |
|
1159 | 1160 | ua = ua.replace('mercurial ', 'mercurial/') |
|
1160 | 1161 | elif ua.startswith('git'): |
|
1161 | pass | |
|
1162 | parts = ua.split(' ') | |
|
1163 | if parts: | |
|
1164 | ua = parts[0] | |
|
1165 | ua = re.sub('\.windows\.\d', '', ua).strip() | |
|
1166 | ||
|
1167 | return ua | |
|
1162 | 1168 | except Exception: |
|
1163 | 1169 | log.exception('Failed to parse scm user-agent') |
|
1170 | if not safe: | |
|
1171 | raise | |
|
1164 | 1172 | |
|
1165 | 1173 | return ua |
@@ -446,4 +446,33 b' class TestGetEnabledHooks(object):' | |||
|
446 | 446 | def test_obfuscate_url_pw(): |
|
447 | 447 | from rhodecode.lib.utils2 import obfuscate_url_pw |
|
448 | 448 | engine = u'/home/repos/malmö' |
|
449 | assert obfuscate_url_pw(engine) No newline at end of file | |
|
449 | assert obfuscate_url_pw(engine) | |
|
450 | ||
|
451 | ||
|
452 | @pytest.mark.parametrize("test_ua, expected", [ | |
|
453 | ("", ""), | |
|
454 | ('"quoted"', 'quoted'), | |
|
455 | ('internal-merge', 'internal-merge'), | |
|
456 | ('hg/internal-merge', 'hg/internal-merge'), | |
|
457 | ('git/internal-merge', 'git/internal-merge'), | |
|
458 | ||
|
459 | # git | |
|
460 | ('git/2.10.1 (Apple Git-78)', 'git/2.10.1'), | |
|
461 | ('GiT/2.37.2.windows.2', 'git/2.37.2'), | |
|
462 | ('git/2.35.1 (Microsoft Windows NT 10.0.19044.0; Win32NT x64) CLR/4.0.30319 VS16/16.0.0', 'git/2.35.1'), | |
|
463 | ('ssh-user-agent', 'ssh-user-agent'), | |
|
464 | ('git/ssh-user-agent', 'git/ssh-user-agent'), | |
|
465 | ||
|
466 | ||
|
467 | # hg | |
|
468 | ('mercurial/proto-1.0 (Mercurial 4.2)', 'mercurial/4.2'), | |
|
469 | ('mercurial/proto-1.0', ''), | |
|
470 | ('mercurial/proto-1.0 (Mercurial 3.9.2)', 'mercurial/3.9.2'), | |
|
471 | ('mercurial/ssh-user-agent', 'mercurial/ssh-user-agent'), | |
|
472 | ('mercurial/proto-1.0 (Mercurial 5.8rc0)', 'mercurial/5.8rc0'), | |
|
473 | ||
|
474 | ||
|
475 | ]) | |
|
476 | def test_user_agent_normalizer(test_ua, expected): | |
|
477 | from rhodecode.lib.utils2 import user_agent_normalizer | |
|
478 | assert user_agent_normalizer(test_ua, safe=False) == expected |
General Comments 0
You need to be logged in to leave comments.
Login now